欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2023-12-30 16:08:16
...

本文翻译自: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? 如何将项目添加到数组?


#1楼

参考:https://stackoom.com/question/i0De/如何在Python中声明和添加项目到数组


#2楼

No, if you do: 不,如果您这样做:

array = {}

IN your example you are using array as a dictionary, not an array. 在您的示例中,您使用array作为字典,而不是数组。 If you need an array, in Python you use lists: 如果需要数组,则在Python中使用列表:

array = []

Then, to add items you do: 然后,要添加项目,请执行以下操作:

array.append('a')

#3楼

{} 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 extend extend列表以包括另一个列表中的元素,请使用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']

#4楼

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 (在其他语言中也称为哈希表,关联的数组等),因此您无需为字典添加“追加”。

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

array = []
array.append(valueToBeInserted)

#5楼

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: 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: 蟒蛇:

mylist = [1,2,3]

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

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

#6楼

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"]添加到它。

相关标签: python arrays

上一篇:

下一篇: