Python列表教程

时间:2020-02-23 14:42:57  来源:igfitidea点击:

在本教程中,我们将看到关于Python列表。

Python列表是Python中最使用的数据结构之一。
Python列表是有序序列中的项目集合。
Python中有6个序列,列表是其中之一。

Python列表以开放方括号开始,并以关闭方括号,[]以闭合方括号结尾,我们可以将多种数据类型放入列表中。

让我们用很多例子学习列表。

创建列表

我们可以使用方括号创建列表。

#Create list with prepopulated elements
list1=[1,2,3,4]
print("List 1:",list1)
#Create an empty list and add elements to it.
list2=[]
list2.append(1)
list2.append(2)
list2.append(3)
list2.append(4)
print("List 2:",list2)

输出:

List 1: [1, 2, 3, 4]
List 2: [1, 2, 3, 4]

访问列表元素

我们可以使用Square Brackets.let访问列表元素。

listOfFruits=['Apple','Orange','Banana','Pineapple']
print("First fruit:",listOfFruits[0])
print("Second fruit:",listOfFruits[1])
print("Third fruit:",listOfFruits[2])
print("Fourth fruit:",listOfFruits[3])

输出:

First fruit: Apple
Second fruit: Orange
Third fruit: Banana
Fourth fruit: Pineapple

列表切片

我们可以使用Slicing与列表中的字符串类似。
使用以下语法进行切片。
list1 [开始:end:step]所有变量开始,结束和步骤是可选的。
启动:它表示列表的开始:它表示列表的结束步骤:它表示访问元素的步骤。
让我们通过示例来理解。

listOfFruits=['Apple','Orange','Banana','Pineapple']
#Print first two elements of the list
print("First two elements:",listOfFruits[:2])
#Print last two items of the list
print("Last two elements:",listOfFruits[2:])
#Print middle elements of the list
print("Middle two elements:",listOfFruits[1:3])
#Print elements in step of 2
print("Elements in step 2",listOfFruits[::2])

输出:

First two elements: ['Apple', 'Orange']
Last two elements: ['Banana', 'Pineapple']
Middle two elements: ['Orange', 'Banana']
Elements in step 2 ['Apple', 'Banana']

请通过上面的示例,我们将能够了解切片Works.let,我们还可以以相反的顺序提供横跨列表的负索引。
-1是列表中的最后一个索引。
让我们通过示例来理解。

listOfFruits=['Apple','Orange','Banana','Pineapple']
#Print last three elements of the list
print("last three elements:",listOfFruits[-3:])
#Print list in reverse order
print("Print list in reverse order:",listOfFruits[::-1])

输出:

last three elements: ['Orange', 'Banana', 'Pineapple']
Print list in reverse order: ['Pineapple', 'Banana', 'Orange', 'Apple']

Python列表方法

请查找可以与Python列表一起使用的方法列表。

方法描述
append此方法用于将单项添加到列表的末尾。
extend此方法用于将项目列表添加到列表的末尾
remove此方法用于从列表中删除元素。如果未存在元素,则会引发错误
index此方法用于查找给定元素的索引
pop此方法将删除并返回传递的索引。如果未传递索引,则将删除并返回列表中的最后一个元素。
count此方法将计算列表中传递元素的发生
clear此方法用于删除列表的所有元素。
copy此方法用于扫描副本列表。
sort此方法用于按升序或者下降顺序对列表进行排序
reverse此方法用于反转列表。
insert此方法用于将元素添加到指定索引处的列表中。

获取列表的长度

我们可以使用Python的LEN函数获取列表的长度。

listOfFruits=['Apple','Orange','Banana','Pineapple']
print('Length of listOfFruits is:',len(listOfFruits))

输出:

Length of listOfFruits is: 4

更改具有索引列表中的值

我们可以使用=运算符在任何索引处更改值。
让我们在简单的例子的帮助下了解。

listOfFruits=['Apple','Orange','Banana','Pineapple']
print("listOfFruits before:",listOfFruits)
listOfFruits[1]='Mango'
print("listOfFruits after:",listOfFruits)

输出:

listOfFruits before: ['Apple', 'Orange', 'Banana', 'Pineapple']
listOfFruits after: ['Apple', 'Mango', 'Banana', 'Pineapple']

如我们所见,列表的索引1的值从"橙色"更改为"芒果"。

列出连接和列表复制

我们可以使用+运算符来连接列表。
请注意,它不会影响原始列表。
如果要对第一个列表进行更改,则需要显式分配它。

让我们在简单的例子的帮助下了解。

list1=['Apple','Orange']
list2=['Banana','Pineapple']
print('ListOfFruits:',list1+list2)

输出:

ListOfFruits: ['Apple', 'Orange', 'Banana', 'Pineapple']

我们可以在*运算符的帮助下复制列表

list1=['Apple','Orange']
print("List 1:",list1*2)

输出:

List 1: ['Apple', 'Orange', 'Apple', 'Orange']