Python 当我尝试从QString数组创建时,创建numpy数组失败

Python 当我尝试从QString数组创建时,创建numpy数组失败,python,numpy,pyqt4,Python,Numpy,Pyqt4,如果数组的大小为2x2或更大,则一切正常,但如果行的维度为1,例如1x2,则numpy会执行我没有预料到的操作 我怎样才能解决这个问题 # TEST 1 OK myarray = np.array([[QString('hello'), QString('world')], [QString('hello'), QString('moon')]], dtype=object) print myarray print m

如果数组的大小为2x2或更大,则一切正常,但如果行的维度为1,例如1x2,则numpy会执行我没有预料到的操作

我怎样才能解决这个问题

# TEST 1 OK
myarray = np.array([[QString('hello'), QString('world')],
                    [QString('hello'), QString('moon')]],
                   dtype=object)
print myarray
print myarray.shape
#[[PyQt4.QtCore.QString(u'hello') PyQt4.QtCore.QString(u'world')]
# [PyQt4.QtCore.QString(u'hello') PyQt4.QtCore.QString(u'moon')]]
#(2, 2)


# TEST 2 OK
myarray = np.array([['hello'], ['world']], dtype=object)
print myarray
print myarray.shape  
#[['hello']
# ['world']]
#(2, 1)


# TEST 3 FAIL
myarray = np.array([[QString('hello'), QString('world')]], dtype=object)
print myarray
print myarray.shape
#[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[PyQt4.QtCore.QString(u'h')]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
#..
#[[[[[[[[[[[[[[[[[[[[[[[[[[[[[PyQt4.QtCore.QString(u'e')]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
# etc...
#(1, 2, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

尝试不同长度的字符串:

np.array([[QString('hello'), QString('moon')]], dtype=object)`.  
或者创建并填充对象数组的方法

A = np.empty((1,2), dtype=object)
A[:] = [QString('hello'), QString('moon')]
我不熟悉这些对象,但在其他情况下,如果我们试图从列表构建对象数组,那么如果列表的长度相同,则会很棘手。如果
QString
是可编辑的,则使用
时,可能会发生类似的情况

我猜您的第一个示例之所以有效,是因为on
QString
比其他示例短,而不是因为它是2x2


最近关于从自定义字典类生成对象数组的问题可能与此相关:

哦,奇怪。。。似乎numpy将字符串视为数组或其他东西。。。如果在两个结束方括号之间加一个逗号会怎么样?没错,我尝试了第一个示例,但如果长度相同,就失败了!。很好的链接,可能是问题的原因,但我认为你的解决方案对我来说已经足够了,谢谢!