我需要一个数组,该数组包含记录数组或记录列表。我需要这样的东西:
people = [['David','Blue','Dog','Car'],['Sally','Yellow','Cat','Boat']]
或:
people = (['David','Blue','Dog','Car'],['Sally','Yellow','Cat','Boat'])
但我不断得到:
people = ['David','Blue','Dog','Car','Sally','Yellow','Cat','Boat']
我尝试过append和concatenate,不同的轴,不同的np初始化,但结果总是一样的。这是我的最新版本。我做错了什么?
import numpy as np
# Tried
# people = np.empty((0,0), dtype='S')
# people = np.array([[]])
people = np.array([])
records = GetRecordsFromDB()
for record in records:
# Do some stuff
# Tried
# person = [name, color, animal, vehicle]
person = np.array([name, color, animal, vehicle])
# Tried this with different axis
# people = np.append(people, person, axis=0)
people = np.concatenate((people, person))
谢谢。
编辑:如果有帮助的话,这将是Pandas DataFrame的输入。
使用np.c_
people = np.c_[people, person]
以下是我解决这个问题的方法:
import numpy as np
people = np.array([])
records = GetRecordsFromDB()
for record in records:
# Do some stuff
person = np.array([name, color, animal, vehicle])
if len(people) == 0:
people = [person]
else:
people = np.append(people, [person], axis=0)
编辑
看着你的回答,我意识到我对你的主题太当真了。[360],下面列出一组列表。但是[356]是一个2d阵列;行";看起来像一个列表,但事实并非如此。
更早
In [354]: alist = [['David','Blue','Dog','Car'],['Sally','Yellow','Cat','Boat']]
...:
In [355]: alist
Out[355]: [['David', 'Blue', 'Dog', 'Car'], ['Sally', 'Yellow', 'Cat', 'Boat']]
In [356]: np.array(alist)
Out[356]:
array([['David', 'Blue', 'Dog', 'Car'],
['Sally', 'Yellow', 'Cat', 'Boat']], dtype='<U6')
这就形成了一个字符串的2d数组。这种结构与教科书中制作二维数字数组的例子没有什么不同:
In [358]: np.array([[1, 2], [3, 4]])
Out[358]:
array([[1, 2],
[3, 4]])
带hstack
或concatenate
:
In [359]: np.hstack(alist)
Out[359]:
array(['David', 'Blue', 'Dog', 'Car', 'Sally', 'Yellow', 'Cat', 'Boat'],
dtype='<U6')
要制作一个只有2个列表的数组,您必须初始化一个:
In [360]: arr = np.empty(2, object)
In [361]: arr
Out[361]: array([None, None], dtype=object)
In [362]: arr[:] = alist
In [363]: arr
Out[363]:
array([list(['David', 'Blue', 'Dog', 'Car']),
list(['Sally', 'Yellow', 'Cat', 'Boat'])], dtype=object)
如果列表的长度不同,
In [364]: np.array([["David", "Blue"], ["Sally", "Yellow", "Cat"]])
<ipython-input-364-be12d6dec312>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
np.array([["David", "Blue"], ["Sally", "Yellow", "Cat"]])
Out[364]:
array([list(['David', 'Blue']), list(['Sally', 'Yellow', 'Cat'])],
dtype=object)
请完整阅读该警告。
默认情况下,np.array
尝试创建一个基类的多维数组,如int或string。只有当它做不到的时候,它才会重新创建对象数据类型数组。这种数组应该被视为二等数组,只有在真正需要的时候才使用。通常情况下,列表也一样好。
您的迭代创建就是这样一种情况
people = []
records = GetRecordsFromDB()
for record in records:
# Do some stuff
# Tried
# person = [name, color, animal, vehicle]
person = np.array([name, color, animal, vehicle])
people = append(person)
只要添加一个引用,就可以将项目(无论是列表还是数组(或其他任何项目(添加到列表中。尝试使用concatenate
添加数组中的项不仅很难正确,而且速度较慢,因为它每次都会生成一个全新的数组。这意味着要大量复制!
np.append
是一种命名不正确的调用concatenate
的方法。它不是list.append
克隆。
使用np.concatenate
需要小心处理尺寸。不要马虎,以为它会弄清楚你想要什么。
类似地,这不是列表[]
:的结束
In [365]: np.array([])
Out[365]: array([], dtype=float64)
In [366]: np.array([]).shape
Out[366]: (0,)
它是一个具有特定形状的1d阵列。您只能将它与另一个1d数组连接起来——一个是唯一的轴,0(。