英文:
How to create an array of empty lists?
问题
我在处理NumPy的内置数组时遇到了一些问题。
我有一个包含数值的列表:
l = ["a", "b", "c", "d", "e"]
列表l
的长度为5,我想创建一个具有以下结构的数组。
array = [[], [], [], [], []]
内部数组的数量应该等于列表l
的长度(5)。
是否有办法在NumPy中实现这个,或者是否可以使用Python内置的列表实现这个目标呢?
英文:
I'm struggling a bit with NumPy's build in arrays.
I have a list with values:
l = ["a", "b", "c", "d", "e"]
The len(l)
of the list is 5 and I want to create an array with the following structure.
array = [[][][][][]]
The inner array amount should be the length of the list l
. (5)
Is there any solution to this with NumPy or is there a solution doing this with the build in list from python.
答案1
得分: 1
使用Python:
array = [[] for _ in range(len(l))]
输出:[[], [], [], [], []]
使用NumPy:
array = np.zeros(shape=(len(l), 0))
输出:array([], shape=(5, 0), dtype=float64)
(这与np.array([[], [], [], [], []])
相同)
英文:
Using python:
array = [[] for _ in range(len(l))]
Output: [[], [], [], [], []]
With numpy:
array = np.zeros(shape=(len(l), 0))
Output: array([], shape=(5, 0), dtype=float64)
(this is the same as np.array([[], [], [], [], []])
)
答案2
得分: 1
使用内置列表:
n = 5
arr = [[] for x in range(n)]
arr
英文:
With bulit in list:
n = 5
arr = [[] for x in range(n)]
arr
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论