如何创建一个空列表的数组?

huangapple go评论65阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月7日 23:21:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定