我需要一個包含記錄陣列或記錄串列的陣列。我需要這樣的東西:
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']
我試過追加 vs 連接,不同的軸,不同的 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 的輸入。
uj5u.com熱心網友回復:
采用np.c_
people = np.c_[people, person]
uj5u.com熱心網友回復:
這是我解決這個問題的方法:
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)
uj5u.com熱心網友回復:
編輯
看著你的回答,我意識到我把你的主題行字面化了。[360] 和以下創建一個串列陣列。但是 [356] 是一個二維陣列,其中每個“行”看起來像一個串列 - 但事實并非如此。
早些時候
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')
這會生成一個二維字串陣列。該構造與制作二維數值陣列的教科書示例沒有什么不同:
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。只有當它做不到時,它才會退回到創建一個物件 dtype 陣列。這種陣列應該被視為二等陣列,只有在真正需要時才使用。通常,串列串列也一樣好。
Your iterative creation is one such case
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)
Items, whether lists or array (or anything else) can be added to a list in-place with just the addition of a reference. Trying use concatenate to add items of an array is, not only harder to get right, but slower, since it is making a whole new array each time. That means a lot of copying!
np.append is a badly named way of calling concatenate. It is not a list.append clone.
Using np.concatenate requires a careful handling of dimensions. Don't be sloppy, thinking it will figure out what you want.
Similarly this is not a close of list []:
In [365]: np.array([])
Out[365]: array([], dtype=float64)
In [366]: np.array([]).shape
Out[366]: (0,)
It is a 1d array with a specific shape. You can only concatenate it with another 1d array - one the only axis, 0).
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/443144.html
上一篇:為什么用陣列索引陣列會回傳兩行?
下一篇:使用Python繪制每個節點的值
