如何在 Python 中获取任意大小的空列表?
我基本上想要一个与 C: 中的这个数组等效的 Python,
int a[x];
但在 python 中我声明了一个像这样的数组:
a = []
问题是我想用像这样的值分配随机槽:
a[4] = 1
但我不能用 Python 做到这一点,因为 Python 列表是空(长度为 0)。
I basically want a Python equivalent of this Array in C:
int a[x];
but in python I declare an array like:
a = []
and the problem is I want to assign random slots with values like:
a[4] = 1
but I can't do that with Python, since the Python list is empty (of length 0).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果“数组”实际上是指 Python 列表,则可以使用
或
If by "array" you actually mean a Python list, you can use
or
你不能在Python中完全做你想做的事(如果我没理解错的话)。您需要为列表(或如您所说的数组)的每个元素添加值。
但是,尝试一下:
对于其他类型的列表,请使用 0 以外的值。
None
通常也是一个不错的选择。You can't do exactly what you want in Python (if I read you correctly). You need to put values in for each element of the list (or as you called it, array).
But, try this:
For lists of other types, use something besides 0.
None
is often a good choice as well.您可以使用 numpy:
空数组中的示例:
You can use numpy:
Example from Empty Array:
您也可以使用列表的扩展方法来扩展它。
also you can extend that with extend method of list.
只需声明列表并附加每个元素即可。例如:
Just declare the list and append each element. For ex:
如果您(或此问题的其他搜索者)实际上有兴趣创建一个连续数组来填充整数,请考虑 bytearray 和 memoryivew:
If you (or other searchers of this question) were actually interested in creating a contiguous array to fill with integers, consider bytearray and memoryivew:
还可以创建一个具有特定大小的空数组:
如果将其定义为 array = [] * n 那么如果您修改一项,由于其可变性,所有项都会以相同的方式更改。
It is also possible to create an empty array with a certain size:
if you define it as
array = [] * n
then if you modify one item, all are changed the same way, because of its mutability.如果你确实想要一个 C 风格的数组,
请注意,Python 中没有未初始化变量的概念。变量是绑定到值的名称,因此该值必须具有某些内容。在上面的示例中,数组用零初始化。
然而,这在 python 中并不常见,除非你确实需要它来处理低级的东西。在大多数情况下,正如其他答案所建议的那样,最好使用空列表或空 numpy 数组。
If you actually want a C-style array
Note that there's no concept of un-initialized variable in python. A variable is a name that is bound to a value, so that value must have something. In the example above the array is initialized with zeros.
However, this is uncommon in python, unless you actually need it for low-level stuff. In most cases, you are better-off using an empty list or empty numpy array, as other answers suggest.
分配“随机槽”的(我认为唯一的)方法是使用字典,例如:
这对于“快速破解”来说很方便,并且如果您没有对数组元素的密集访问,则查找开销是无关紧要的。
否则,使用固定大小的预分配(例如 numpy)数组会更有效,您可以使用
a = np.empty(10)
创建该数组(对于未初始化的向量长度为 10) 或a = np.zeros([5,5])
(对于用零初始化的 5x5 矩阵)。备注:在您的 C 示例中,您还必须在分配(不是这样)“随机槽”(即 0 和 x 之间的整数索引)之前分配数组(您的
int a[x];
) -1)。参考资料:
dict
数据类型:https ://docs.python.org/3/library/stdtypes.html#mapping-types-dictThe (I think only) way to assign "random slots" is to use a dictionary, e.g.:
This can be handy for "quick hacks", and the lookup overhead is irrelevant if you don't have intensive access to the array's elements.
Otherwise, it will be more efficient to work with pre-allocated (e.g., numpy) arrays of fixed size, which you can create with
a = np.empty(10)
(for an non-initialized vector of length 10) ora = np.zeros([5,5])
for a 5x5 matrix initialized with zeros).Remark: in your C example, you also have to allocate the array (your
int a[x];
) before assigning a (not so) "random slot" (namely, integer index between 0 and x-1).References:
dict
datatype: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict