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

问题:如何在Python中声明和添加项目到数组?

我试图将项目添加到python中的数组。

我跑

array = {}

然后,我尝试通过以下操作向此数组添加一些内容:

array.append(valueToBeInserted)

似乎没有.append办法。如何将项目添加到数组?

I’m trying to add items to an array in 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. How do I add items to an array?


回答 0

{}表示一个空字典,而不是数组/列表。对于列表或数组,您需要[]

要初始化一个空列表,请执行以下操作:

my_list = []

要么

my_list = list()

要将元素添加到列表,请使用 append

my_list.append(12)

extend在列表中包含另一个列表中的元素,请使用extend

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

要从列表中删除元素,请使用 remove

my_list.remove(2)

字典表示键/值对的集合,也称为关联数组或映射。

要初始化一个空字典,请使用{}dict()

字典具有键和值

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

要使用其他字典的内容扩展字典,可以使用以下update方法

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

从字典中删除值

del my_dict['key']

{} 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

my_list.append(12)

To extend the list to include the elements from another list use extend

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

To remove an element from a list use 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()

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

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

To remove a value from a dictionary

del my_dict['key']

回答 1

不,如果您这样做:

array = {}

在您的示例中,您使用的array是字典,而不是数组。如果需要数组,则在Python中使用列表:

array = []

然后,要添加项目,请执行以下操作:

array.append('a')

No, if you do:

array = {}

IN your example you are using array as a dictionary, not an array. If you need an array, in Python you use lists:

array = []

Then, to add items you do:

array.append('a')

回答 2

数组(list在python中称为)使用该[]符号。{}是用于dict(在其他语言中也称为哈希表,关联的数组等),因此您无需为字典添加“追加”。

如果您实际上想要一个数组(列表),请使用:

array = []
array.append(valueToBeInserted)

Arrays (called list in python) use the [] notation. {} is for dict (also called hash tables, associated arrays, etc in other languages) so you won’t have ‘append’ for a dict.

If you actually want an array (list), use:

array = []
array.append(valueToBeInserted)

回答 3

仅出于完成目的,您还可以执行以下操作:

array = []
array += [valueToBeInserted]

如果它是一个字符串列表,这也将起作用:

array += 'string'

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'

回答 4

在某些语言(例如JAVA)中,您可以使用花括号定义数组,如下所示,但在python中,其含义不同:

Java:

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

但是,在Python中,花括号用于定义字典,需要将key:value赋值设置为{'a':1, 'b':2}

要实际定义一个数组(在python中实际上称为list),您可以执行以下操作:

Python:

mylist = [1,2,3]

或其他示例,例如:

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

In some languages like JAVA you define an array using curly braces as following but in python it has a different meaning:

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}

To actually define an array (which is actually called list in python) you can do:

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]

回答 5

您也可以:

array = numpy.append(array, value)

请注意,该numpy.append()方法返回一个新对象,因此,如果要修改初始数组,则必须编写:array = ...

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 = ...


回答 6

我相信你们都错了。您需要执行以下操作:

array = array[] 为了定义它,然后:

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

I believe you are all wrong. you need to do:

array = array[] in order to define it, and then:

array.append ["hello"] to add to it.