如何在python中声明0的数组(或特定大小的数组)[duplicate]

时间:2022-01-01 01:21:50

This question already has an answer here:

这个问题已经有了答案:

I am trying to build a histogram of counts... so I create buckets. I know I could just go through and append a bunch of zeros i.e something along these lines:

我正在尝试建立一个计数的直方图。所以我创建桶。我知道我可以把一串0加进去。e有一些类似的东西:

buckets = []
for i in xrange(0,100):
    buckets.append(0)

Is there a more elegant way to do it? I feel like there should be a way to just declare an array of a certain size.

有没有一种更优雅的方法呢?我觉得应该有一种方法来声明一个特定大小的数组。

I know numpy has numpy.zeros but I want the more general solution

我知道numpy有numpy。0,但我想要更一般的解

10 个解决方案

#1


266  

buckets = [0] * 100

#2


79  

Just for completeness: To declare a multidimensional list of zeros in python you have to use a list comprehension like this:

为了完整:要在python中声明一个多维的0列表,您必须使用如下的列表理解:

buckets = [[0 for col in range(5)] for row in range(10)]

to avoid reference sharing between the rows.

避免行之间的引用共享。

This looks more clumsy than chester1000's code, but is essential if the values are supposed to be changed later. See the Python FAQ for more details.

这看起来比chester1000的代码更加笨拙,但是如果要在以后修改这些值,这是非常必要的。有关更多细节,请参见Python FAQ。

#3


17  

Use this:

用这个:

bucket = [None] * 100
for i in range(100):
    bucket[i] = [None] * 100

OR

w, h = 100, 100
bucket = [[None] * w for i in range(h)]

Both of them will output proper empty multidimensional bucket list 100x100

两者都将输出适当的空多维bucket列表100x100

#4


16  

You can multiply a list by an integer n to repeat the list n times:

你可以将一个列表乘以一个整数n来重复这个列表n次:

buckets = [0] * 100

#5


11  

use numpy

使用numpy

import numpy
zarray = numpy.zeros(100)

And then use the Histogram library function

然后使用直方图库函数

#6


3  

The simplest solution would be

最简单的解决办法是

"\x00" * size # for a buffer of binary zeros
[0] * size # for a list of integer zeros

In general you should use more pythonic code like list comprehension (in your example: [0 for unused in xrange(100)]) or using string.join for buffers.

一般来说,您应该使用更多的python代码,如列表理解(在您的示例中:[0在xrange(100)中未使用))或使用字符串。加入缓冲。

#7


1  

The question says "How to declare array of zeros ..." but then the sample code references the Python list:

问题是“如何声明0的数组…”但是示例代码引用了Python列表:

buckets = []   # this is a list

However, if someone is actually wanting to initialize an array, I suggest:

但是,如果有人真的想要初始化一个数组,我建议:

from array import array

my_arr = array('I', [0] * count)

The Python purist might claim this is not pythonic and suggest:

Python的纯粹主义者可能会说这不是Python的,并建议:

my_arr = array('I', (0 for i in range(count)))

The pythonic version is very slow and when you have a few hundred arrays to be initialized with thousands of values, the difference is quite noticeable.

python版本非常慢,当您有几百个数组要用数千个值初始化时,差异是非常明显的。

#8


0  

Depending on what you're actually going to do with the data after it's collected, collections.defaultdict(int) might be useful.

根据收集数据之后的实际操作,collection .defaultdict(int)可能是有用的。

#9


-1  

Well I would like to help you by posting a sample program and its output

我想通过发布一个示例程序及其输出来帮助你们

Program :-

程序:

t=input("")

x=[None]*t

y=[[None]*t]*t

for i in range(1,t+1):

      x[i-1]=i;
      for j in range(1,t+1):
            y[i-1][j-1]=j;

print x

print y

Output :-

输出:

2

[1, 2]

[[1, 2], [1, 2]]

I hope this clears some very basic concept of yours regarding their declaration. To initialize them with some other specific values,like initializing them with 0..you can declare them as :

我希望这能澄清你关于他们声明的一些非常基本的概念。用其他特定的值初始化它们,比如用0初始化它们。您可以将其声明为:

x=[0]*10

x =[0]* 10

Hope it helps..!! ;)

希望它帮助. . ! !,)

#10


-2  

If you need more columns:

如果你需要更多的栏目:

buckets = [[0., 0., 0., 0., 0.] for x in range(0)]

#1


266  

buckets = [0] * 100

#2


79  

Just for completeness: To declare a multidimensional list of zeros in python you have to use a list comprehension like this:

为了完整:要在python中声明一个多维的0列表,您必须使用如下的列表理解:

buckets = [[0 for col in range(5)] for row in range(10)]

to avoid reference sharing between the rows.

避免行之间的引用共享。

This looks more clumsy than chester1000's code, but is essential if the values are supposed to be changed later. See the Python FAQ for more details.

这看起来比chester1000的代码更加笨拙,但是如果要在以后修改这些值,这是非常必要的。有关更多细节,请参见Python FAQ。

#3


17  

Use this:

用这个:

bucket = [None] * 100
for i in range(100):
    bucket[i] = [None] * 100

OR

w, h = 100, 100
bucket = [[None] * w for i in range(h)]

Both of them will output proper empty multidimensional bucket list 100x100

两者都将输出适当的空多维bucket列表100x100

#4


16  

You can multiply a list by an integer n to repeat the list n times:

你可以将一个列表乘以一个整数n来重复这个列表n次:

buckets = [0] * 100

#5


11  

use numpy

使用numpy

import numpy
zarray = numpy.zeros(100)

And then use the Histogram library function

然后使用直方图库函数

#6


3  

The simplest solution would be

最简单的解决办法是

"\x00" * size # for a buffer of binary zeros
[0] * size # for a list of integer zeros

In general you should use more pythonic code like list comprehension (in your example: [0 for unused in xrange(100)]) or using string.join for buffers.

一般来说,您应该使用更多的python代码,如列表理解(在您的示例中:[0在xrange(100)中未使用))或使用字符串。加入缓冲。

#7


1  

The question says "How to declare array of zeros ..." but then the sample code references the Python list:

问题是“如何声明0的数组…”但是示例代码引用了Python列表:

buckets = []   # this is a list

However, if someone is actually wanting to initialize an array, I suggest:

但是,如果有人真的想要初始化一个数组,我建议:

from array import array

my_arr = array('I', [0] * count)

The Python purist might claim this is not pythonic and suggest:

Python的纯粹主义者可能会说这不是Python的,并建议:

my_arr = array('I', (0 for i in range(count)))

The pythonic version is very slow and when you have a few hundred arrays to be initialized with thousands of values, the difference is quite noticeable.

python版本非常慢,当您有几百个数组要用数千个值初始化时,差异是非常明显的。

#8


0  

Depending on what you're actually going to do with the data after it's collected, collections.defaultdict(int) might be useful.

根据收集数据之后的实际操作,collection .defaultdict(int)可能是有用的。

#9


-1  

Well I would like to help you by posting a sample program and its output

我想通过发布一个示例程序及其输出来帮助你们

Program :-

程序:

t=input("")

x=[None]*t

y=[[None]*t]*t

for i in range(1,t+1):

      x[i-1]=i;
      for j in range(1,t+1):
            y[i-1][j-1]=j;

print x

print y

Output :-

输出:

2

[1, 2]

[[1, 2], [1, 2]]

I hope this clears some very basic concept of yours regarding their declaration. To initialize them with some other specific values,like initializing them with 0..you can declare them as :

我希望这能澄清你关于他们声明的一些非常基本的概念。用其他特定的值初始化它们,比如用0初始化它们。您可以将其声明为:

x=[0]*10

x =[0]* 10

Hope it helps..!! ;)

希望它帮助. . ! !,)

#10


-2  

If you need more columns:

如果你需要更多的栏目:

buckets = [[0., 0., 0., 0., 0.] for x in range(0)]