python中定义数据结构
This article is about lists. They are the most versatile and resourceful, in-built data structure in Python. They can simultaneously hold heterogeneous data i.e., integers, floats, strings, NaN, Booleans, functions, etc. within the same list. They are an ordered sequence of items that means the order of the elements is preserved while accessing lists. They are mutable i.e., you can change (add, delete, modify) any item in the list. They can hold duplicate items unlike “sets” — another data structure in Python.
本文是关于列表的 。 它们是Python中功能最多,资源最丰富的内置数据结构 。 它们可以同时保存异构数据,即同一列表中的整数,浮点数,字符串,NaN,布尔值,函数等。 它们是项目的有序序列,这意味着在访问列表时会保留元素的顺序。 它们是可变的,即您可以更改 (添加,删除,修改)列表中的任何项目。 他们可以保留与“集合”不同的重复项,而“集合”是Python中的另一种数据结构。
After reading this article, you will gain a clear understanding and ability to work at an advanced level with Python lists.
阅读本文之后,您将获得对Python列表的更深入的了解和更高级的工作能力。
I will cover the following topics:
我将介绍以下主题:
Creating a list and adding elements
创建列表并添加元素
Accessing list elements
访问列表元素
Removing list elements
删除列表元素
Inserting elements
插入元素
List arithmetic
列表算术
Reversing a list
倒转清单
Sorting a list
排序清单
Index of an item
项目索引
Counting item frequency in a list
计算列表中的项目频率
List comprehensions
清单理解
Copying a list
复制清单
Nested lists
嵌套列表
1)创建一个列表并添加元素 (1) Creating a list and adding elements)
First, we initialize an empty list called “data”. The list is created using square brackets. Naturally, the length of an empty list is zero.
首先,我们初始化一个称为“数据”的空列表 。 该列表使用方括号创建。 自然,空列表的长度为零。
data = []len(data)
>>> 0# Check the type of the variable 'data'
type(data)
>>> list
Now let’s add our very first element to this list. This is done using the append() function. You will notice that its length now becomes one.
现在,让我们将第一个元素添加到此列表中。 这是使用append()函数完成的。 您会注意到它的长度现在变为1。
data.append(100)data
>>> [100]len(data)
>>> 1
Let’s add a second element. It will be appended (added) at the end of the list. Similarly, you can append as many elements as you want.
让我们添加第二个元素。 它将被添加(添加)在列表的末尾。 同样,您可以根据需要附加任意数量的元素。
data.append(200)
data
>>> [100, 200]len(data)
>>> 2data.append(300)
data
>>> [100, 200, 300]
The append function is useful when you do not know beforehand how many elements will be in your list. For example, to store the number of people entering a shop every hour, you need to append the number of customers on an hourly basis. However, if you just hosted an exam, you know exactly how many students wrote the exam. Now, if you want to store their grades in a list, instead of appending, you can just initialize your list altogether.
当您事先不知道列表中有多少个元素时, append函数很有用。 例如,要存储每小时进入商店的人数,您需要每小时附加一次客户数量。 但是,如果您只是主持考试,则可以确切知道有多少学生参加了考试。 现在,如果要将其成绩存储在列表中,而不是追加,则可以完全初始化列表。
grades = [70, 100, 97, 67, 85]len(grades)
>>> 5
Don’t worry! You can still add more elements to your already initialized list using append. Just simply use data.append(80)
to add the grades of a sixth student afterward and it will be appended at the end of the list.
不用担心 您仍然可以使用append将更多元素添加到已初始化的列表中。 只需简单地使用data.append(80)
添加第六名学生的成绩,它就会被添加到列表的末尾。
如何一次添加两个或更多学生的分数? (How to add the marks of two or more students at once?)
Suppose you want to append marks of three students simultaneously. You cannot use grades.append(99, 100, 95) because “append” takes exactly one argument. You will have to use the “append” function three times.
假设你想同时追加三个学生的痕迹。 您不能使用grades.append(99,100,95),因为“ append”仅接受一个参数 。 您将不得不使用“附加”功能三次。
Rather than appending three times, you can use extend() in such cases. You need to put the three elements in a tuple form (an iterable).
在这种情况下,您可以使用extend()而不是附加三次。 您需要将三个元素以元组形式(可迭代)放置。
Note that you cannot use extend for appending a single element i.e., data.extend((90))
won't work.
请注意,您不能将扩展用于附加单个元素,即data.extend((90))
将不起作用。
grades = [70, 100, 97, 67, 85]
grades.extend((99, 100, 95))
print (grades)
>>> [70, 100, 97, 67, 85, 99, 100, 95]
现在您会问:“为什么我们不能一次添加三个年级?” (Now you will ask, “Why can’t we append three grades at once?”)
You can, but there is a catch. As shown below, the three grades inserted together show up as a list inside the main list. Such lists are called “Nested Lists”. I will show more examples in the last section of this article.
可以, 但是有一个陷阱 。 如下所示,插入在一起的三个年级在主列表中显示为一个列表。 这样的列表称为“嵌套列表” 。 我将在本文的最后一部分中显示更多示例。
grades = [70, 100, 97, 67, 85]
grades.append([99, 100, 95])
print (grades)
>>> [70, 100, 97, 67, 85, [99, 100, 95]] # A nested list
2)访问列表元素 (2) Accessing list elements)
If you are working with data structures, it is very useful to know the concept of indexing. You can think of indexing as a serial number assigned to each element of the list. Simply put, it is similar to your roll numbers in a class.
如果 您正在使用数据结构时,了解概念非常有用 索引 。 您可以将索引视为分配给列表的每个元素的序列号 。 简而言之,它类似于您在课程中的卷数。
The most important thing to know is that indexing in Python starts at 0.
要知道 的 最重要的 事情是 Python