在Python3中如何声明数组

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

How to declare arrays in python3

问题

我想在python3中声明一个数组。我尝试了但出现了错误。

Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> ip_pb=[]
>>> ip_pb[0]="0111111100000000000000011110001"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>>

之后,我做了这个,它可以工作。

>>> ip_pb=[""]
>>> ip_pb[0]="0111111100000000000000011110001"
>>> print(ip_pb)
['0111111100000000000000011110001']

但是,我正在寻找另一种方法。如果我们不知道数组中有多少个值,就不能使用上述方法声明数组。

英文:

I want to declare an array in python3. I tried but I got an error

Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; ip_pb=[]
&gt;&gt;&gt; ip_pb[0]=&quot;0111111100000000000000011110001&quot;
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
IndexError: list assignment index out of range
&gt;&gt;&gt; 

After,I did this,it is working

&gt;&gt;&gt; ip_pb=[&quot;&quot;]
&gt;&gt;&gt; ip_pb[0]=&quot;0111111100000000000000011110001&quot;
&gt;&gt;&gt; print(ip_pb)
[&#39;0111111100000000000000011110001&#39;]

But,I am looking for an another method. If we don't know how many values in the array, we can't declare the array in above method.

答案1

得分: 1

根据你的最后一行(你不知道数组中有多少元素),你想要创建一个空数组,然后使用append()函数将值添加到其中。

在你提供的问题中,代码的修改版本如下:

ip_pb = []
ip_pb.append("0111111100000000000000011110001")
print(ip_pb)
英文:

Based on your last line (that you don't know how many elements are there in the array), you want to create an empty array and then use the append() function to add values into it.

The modified version of the code which you have provided in the question:

ip_pb = []
ip_pb.append(&quot;0111111100000000000000011110001&quot;)
print(ip_pb)

答案2

得分: 1

在Python中,我们通常称之为 list 而不是传统的 array
Python中的 list 总是动态增长的,这意味着与所谓的 array 相比,它没有固定的大小。

对于固定大小的 array,我相信Python本身有一个标准库,您可以使用 import array,但不幸的是它有限的数据类型。
array

为了简单起见,也许您可以尝试这种方法:

def array(fill, n):
    return [fill] * n

a = array("", 10)
a[0] = "10010101"
a[9] = "10230123"
a[10] # IndexError
英文:

In Python, we usually call it list instead of the traditional array.
list in Python is always a dynamically lengthened, that means it doesn't have a fixed size as opposed to what called array.

for fixed size array, I believe Python itself has a standard library which you can use it with import array but unfortunately it has limited data types.
array

For the sake of simplicity, maybe you can just try this method:

def array(fill, n):
    return [fill] * n

a = array(&quot;&quot;, 10)
a[0] = &quot;10010101&quot;
a[9] = &quot;10230123&quot;
a[10] # IndexError

答案3

得分: 0

你应该在这种方式下初始化你的数组:

>>> ip_pb = [None] * 5
>>> ip_pb
[None, None, None, None, None]
>>> ip_pb[1] = 3
>>> ip_pb
[None, 3, None, None, None]
英文:

You should initialize your array if you want to use in this way:

&gt;&gt;&gt; ip_pb = [None] * 5
&gt;&gt;&gt; ip_pb
[None, None, None, None, None]
&gt;&gt;&gt; ip_pb[1] = 3
&gt;&gt;&gt; ip_pb
[None, 3, None, None, None]

答案4

得分: 0

  1. 创建一个空列表(数组),如 x = []
  2. 添加任意数量的元素,如 x.append(25)x.append(100)

您无需预先知道元素的数量,但如果想知道列表中的元素数量,请使用

print(len(x))

要打印完整的列表,请使用

print(x)

要逐个打印每个元素

for i in x:
    print(i)
英文:
  1. Create empty list(array) like x = []
  2. Add any number of elements like x.append(25), x.append(100)

You don't need to know the number of elements beforehand, but if you want to know the count of elements in your list then use

print(len(x))

To print complete list use

print(x)

To print each element separately

for i in x:
    print(i)

huangapple
  • 本文由 发表于 2020年1月4日 12:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587931.html
匿名

发表评论

匿名网友

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

确定