简体   繁体   English

如何在 Python 中向数组声明和添加项目?

[英]How to declare and add items to an array in Python?

I'm trying to add items to an array in python.我正在尝试将项目添加到 python 中的数组。

I run我跑

array = {}

Then, I try to add something to this array by doing:然后,我尝试通过执行以下操作向该数组添加一些内容:

array.append(valueToBeInserted)

There doesn't seem to be a .append method for this.似乎没有.append方法。 How do I add items to an array?如何将项目添加到数组?

{} represents an empty dictionary, not an array/list. {}表示空字典,而不是数组/列表。 For lists or arrays, you need [] .对于列表或数组,您需要[]

To initialize an empty list do this:要初始化一个空列表,请执行以下操作:

my_list = []

or或者

my_list = list()

To add elements to the list, use append要将元素添加到列表中,请使用append

my_list.append(12)

To extend the list to include the elements from another list use extendextend列表以包含另一个列表中的元素,请使用extend

my_list.extend([1,2,3,4])
my_list
--> [12,1,2,3,4]

To remove an element from a list use remove要从列表中remove元素,请使用remove

my_list.remove(2)

Dictionaries represent a collection of key/value pairs also known as an associative array or a map.字典表示键/值对的集合,也称为关联数组或映射。

To initialize an empty dictionary use {} or dict()要初始化一个空字典,请使用{}dict()

Dictionaries have keys and values字典有键和值

my_dict = {'key':'value', 'another_key' : 0}

To extend a dictionary with the contents of another dictionary you may use the update method要使用另一个字典的内容扩展字典,您可以使用update方法

my_dict.update({'third_key' : 1})

To remove a value from a dictionary从字典中删除一个值

del my_dict['key']

If you do it this way:如果你这样做:

array = {}

you are making a dictionary, not an array.您正在制作字典,而不是数组。

If you need an array (which is called a list in python ) you declare it like this:如果您需要一个数组(在 python 中称为列表),您可以像这样声明它:

array = []

Then you can add items like this:然后你可以添加这样的项目:

array.append('a')

Arrays (called list in python) use the [] notation.数组(在 Python 中称为list )使用[]表示法。 {} is for dict (also called hash tables, associated arrays, etc in other languages) so you won't have 'append' for a dict. {}用于dict (在其他语言中也称为哈希表、关联数组等),因此您不会对 dict 进行“附加”。

If you actually want an array (list), use:如果您确实想要一个数组(列表),请使用:

array = []
array.append(valueToBeInserted)

Just for sake of completion, you can also do this:只是为了完成,你也可以这样做:

array = []
array += [valueToBeInserted]

If it's a list of strings, this will also work:如果它是一个字符串列表,这也将起作用:

array += 'string'

In some languages like JAVA you define an array using curly braces as following but in python it has a different meaning:在某些语言(如JAVA)中,您使用花括号定义数组,如下所示,但在 python 中,它具有不同的含义:

Java:爪哇:

int[] myIntArray = {1,2,3};
String[] myStringArray = {"a","b","c"};

However, in Python, curly braces are used to define dictionaries, which needs a key:value assignment as {'a':1, 'b':2}但是,在 Python 中,大括号用于定义字典,需要将key:value赋值为{'a':1, 'b':2}

To actually define an array (which is actually called list in python) you can do:要实际定义一个数组(在 python 中实际上称为 list),您可以执行以下操作:

Python: Python:

mylist = [1,2,3]

or other examples like:或其他示例,例如:

mylist = list()
mylist.append(1)
mylist.append(2)
mylist.append(3)
print(mylist)
>>> [1,2,3]

You can also do:你也可以这样做:

array = numpy.append(array, value)

Note that the numpy.append() method returns a new object, so if you want to modify your initial array, you have to write: array = ...注意numpy.append()方法返回一个新对象,所以如果你想修改你的初始数组,你必须写: array = ...

I believe you are all wrong.我相信你们都错了。 you need to do:你需要做:

array = array[] in order to define it, and then: array = array[]来定义它,然后:

array.append ["hello"] to add to it. array.append ["hello"]添加到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM