数据类型-列表List


实现python中的List以存储各种类型数据的序列。但是,python包含六种能够存储序列的数据类型,但最常见和可靠的类型是列表。

列表可以定义为值集合或不同类型的项目。列表中的项目用逗号(,)分隔,并用方括号[]括起来。


列表可以定义如下:

L1 = ["John", 102, "USA"]
L2 = [1, 2, 3, 4, 5, 6]
L3 = [1, "Ryan"]

如果我们尝试打印L1,L2和L3的类型,那么它将成为一个列表。

让我们考虑一个合适的例子来定义一个列表并打印它的值。

emp = ["John", 102, "USA"] 
Dep1 = ["CS",10];
Dep2 = ["IT",11];
HOD_CS = [10,"Mr. Holding"]
HOD_IT = [11, "Mr. Bewon"]
print("printing employee data...");
print("Name : %s, ID: %d, Country: %s"%(emp[0],emp[1],emp[2]))
print("printing departments...");
print("Department 1:\nName: %s, ID: %d\nDepartment 2:\nName: %s, ID: %s"%(Dep1[0],Dep2[1],Dep2[0],Dep2[1]));
print("HOD Details ....");
print("CS HOD Name: %s, Id: %d"%(HOD_CS[1],HOD_CS[0]));
print("IT HOD Name: %s, Id: %d"%(HOD_IT[1],HOD_IT[0]));
print(type(emp),type(Dep1),type(Dep2),type(HOD_CS),type(HOD_IT));

输出:

printing employee data...
Name : John, ID: 102, Country: USA
printing departments...
Department 1:
Name: CS, ID: 11
Department 2:
Name: IT, ID: 11
HOD Details ....
CS HOD Name: Mr. Holding, Id: 10
IT HOD Name: Mr. Bewon, Id: 11
<class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'>

列出索引和拆分

索引的处理方式与字符串的处理方式相同。可以使用切片运算符[]访问列表的元素。

索引从0开始并转到长度 - 1.列表的第一个元素存储在第0个索引处,列表的第二个元素存储在第1个索引处,依此类推。

请参考下图:

未命名3图片.png

与其他语言不同,python为我们提供了使用否定索引的灵活性。负指数从右侧计算。列表的最后一个元素(最右边)具有索引-1,其相邻的左元素出现在索引-2处,依此类推,直到遇到最左边的元素。

360截图165208167910874.png

更新列表值

列表是python中最通用的数据结构,因为它们是不可变的,并且可以使用切片和赋值运算符更新它们的值。

Python还为我们提供了append()方法,该方法可用于向字符串添加值。

请考虑以下示例来更新列表中的值。

List = [1, 2, 3, 4, 5, 6]   
print(List)   
List[2] = 10;  
print(List)  
List[1:3] = [89, 78]   
print(List)

输出:

[1,2,3,4,5,6]
[1,2,10,4,5,6]
[1,89,78,4,5,6]

也可以使用del关键字删除列表元素。如果我们不知道要从列表中删除哪个元素,Python还为我们提供了remove()方法。

请根据以下示例来学习删除列表元素。

List = [0,1,2,3,4]   
print(List)  
del List[0]  
print(List)   
del List[3]  
print(List)

输出:

[0,1,2,3,4]
[1,2,3,4]
[1,2,3]

Python列表操作

串联(+)和重复(*)运算符的工作方式与处理字符串的方式相同。

让我们看看列表如何处理运算符:

假设列表 l1 = [1, 2, 3, 4], l2 = [5, 6, 7, 8]  

操作符
描述示例
重复
重复运算符使列表元素能够重复多次。
L1 * 2 = [1,2,3,4,1,2,3,4]
级联
它连接运算符两侧提到的列表。
l1 + l2 = [1,2,3,4,5,6,7,8]

 如果特定列表中存在特定项目,则返回true,否则返回false。
print(l1中的2)打印True。
迭代
for循环用于迭代列表元素。

for i in l1:
   print(i)

输出:

1

2

3

4

长度 它用于获取列表的长度

len(l1) = 4  

 

迭代列表

可以使用for-in循环迭代列表。包含四个字符串的简单列表可以如下迭代。

List = ["John", "David", "James", "Jonathan"]  
for i in List: #i will iterate over the elements of the List and contains each element in each iteration.   
    print(i);


Output:


John
David
James
Jonathan


将元素添加到列表中

Python提供了append()函数,我们可以使用它来向列表添加元素。但是,append()方法只能将值添加到列表的末尾。

考虑以下示例,其中,我们从用户获取列表的元素并在控制台上打印列表。

l =[];  
n = int(input("Enter the number of elements in the list")); #Number of elements will be entered by the user  
for i in range(0,n): # for loop to take the input  
    l.append(input("Enter the item?")); # The input is taken from the user and added to the list as the item  
print("printing the list items....");   
for i in l: # traversal loop to print the list items  
    print(i, end = "  ");


Output:

Enter the number of elements in the list 5
Enter the item?1
Enter the item?2
Enter the item?3
Enter the item?4
Enter the item?5
printing the list items....
1  2  3  4  5



从列表中删除元素

List = [0,1,2,3,4]   
print("printing original list: ");  
for i in List:  
    print(i,end=" ")  
List.remove(0)  
print("\nprinting the list after the removal of first element...")  
for i in List:  
    print(i,end=" ")


Output:


printing original list: 
0 1 2 3 4 
printing the list after the removal of first element...
1 2 3 4



Python列表内置函数

Python提供了以下内置函数,可以与列表一起使用。

SN函数描述
1cmp(list1, list2)
它比较了两个列表的元素。
2 len(list)
它用于计算列表的长度。
3 max(list)
它返回列表的最大元素。
4min(list)
它返回列表的最小元素。
5list(seq)
它将任何序列转换为列表。



Python List内置方法

SN函数描述
1list.append(OBJ)
对象obj表示的元素被添加到列表中。
2list.clear()
它会从列表中删除所有元素。
3List.copy()
它返回列表的浅表副本。
4list.count(OBJ)
它返回列表中指定对象的出现次数。
5list.extend(SEQ)
对象seq表示的序列扩展到列表。
6list.index(OBJ)
它返回对象出现的列表中的最低索引。
7list.insert(index,obj)
该对象将插入到指定索引的列表中。
8list.pop(OBJ =列表[-1])
它删除并返回列表的最后一个对象。
9list.remove(OBJ)
它从列表中删除指定的对象。
10list.reverse()
它颠倒了清单。
11list.sort([FUNC])
如果给定,它使用指定的比较函数对列表进行排序。