python列表

列表介绍


python内置的一种数据类型,list。列表是一种有序集合可以随时添加和删除其中的元素

列表中的元素可以是字符串、数字、另一种类型及不同类型的组合,可以为任意类型

In [9]: classmates = ["zyj","sl"]

In [10]: classmates
Out[10]: ['zyj', 'sl']

In [11]: classmates[0]
Out[11]: 'zyj'

In [12]: classmates[-1]
Out[12]: 'sl'

In [13]: len(classmates)
Out[13]: 2

In [14]: 

In [14]: classmates[3]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-14-81a27e3ce05f> in <module>()
----> 1 classmates[3]

IndexError: list index out of range
In [17]: names=[]  #声明一个列表

In [18]: names
Out[18]: []

列表操作:增删改查

1、添加元素 append extend insert

通过append可以向列表中添加元素;

通过extend可以将另一个集合中的元素逐一添加到列表中;

通过insert在列表中的任意位置插入

修改元素的时候,通过下标来确定要修改的是哪个元素,然后才能修改。

In [20]: names=[1,2,3,"100",100]

In [21]: names
Out[21]: [1, 2, 3, '100', 100]

In [22]: names.append("600") #追加

In [23]: names
Out[23]: [1, 2, 3, '100', 100, '600']

In [24]: name2=[1,2]

In [25]: names.extend(name2) #合并

In [26]: names
Out[26]: [1, 2, 3, '100', 100, '600', 1, 2]

In [27]: names.insert(3,4) #插入

In [28]: names
Out[28]: [1, 2, 3, 4, '100', 100, '600', 1, 2]

In [31]: names.insert(2,"777") #插入

In [32]: names
Out[32]: [1, 2, '777', 3, 4, '10', '100', 100, '600', 1, 2]

In [33]: names[2]="888"  #修改

In [34]: names
Out[34]: [1, 2, '888', 3, 4, '10', '100', 100, '600', 1, 2]

查找元素(in, not in, index ,count)即查看指定的元素是否存在

In [45]: names
Out[45]: [1, 2, '888', '888', 4, '10', '100', 100, '600', 1, 2]

In [46]: names.count("888")
Out[46]: 2

In [47]: names.index("888")
Out[47]: 2

In [48]: "888" in names  #返回布尔值,一般做判断用
Out[48]: True

In [49]: "888" not in names
Out[49]: False

删除元素(del,pop,remove)

del:根据下标进行删除,可以删除所有变量。属于内置函数,不属于列表的方法。

pop:默认删除最后一个元素

remove:根据元素的值删除第一个

In [53]: names
Out[53]: [1, 2, '888', '888', 4, '10', '100', 100, '600', 1, 2]

In [54]: del names[1] #删除列表中下标对应的元素

In [55]: names
Out[55]: [1, '888', '888', 4, '10', '100', 100, '600', 1, 2]

In [51]: del name2 #直接删除一个列表

In [52]: name2
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-52-e1187e69ab52> in <module>()
----> 1 name2

NameError: name 'name2' is not defined

In [53]: 

In [58]: names
Out[58]: [1, '888', '888', 4, '10', '100', 100, '600', 1]

In [59]: names.pop()  #删除最后一个元素并返回最后一个元素的值
Out[59]: 1

In [64]: names
Out[64]: [1, '888', '888', 4, '10', 100, '600']

In [65]: names.remove(4) #删除指定的元素

In [66]: names
Out[66]: [1, '888', '888', '10', 100, '600']

排序(sort reverse)

sort方法是将list按特定顺序重新排列,默认由小到大,参数reverse=True可改为倒序,由大到小。

reverse方法是将list逆置

In [67]: names
Out[67]: [1, '888', '888', '10', 100, '600']

In [68]: names.reverse()

In [69]: names
Out[69]: ['600', 100, '10', '888', '888', 1]

In [70]: names[-1::-1]
Out[70]: [1, '888', '888', '10', 100, '600']

In [71]: names.sort()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-71-b5d03e0d37c6> in <module>()
----> 1 names.sort()

TypeError: '<' not supported between instances of 'int' and 'str'

In [72]: name=["zyj","sl","dsf"]

In [73]: name.sort()

In [74]: name
Out[74]: ['dsf', 'sl', 'zyj']

In [75]: num=[1,4,6,2,9,1,34]

In [78]: num.sort()

In [79]: num
Out[79]: [1, 1, 2, 4, 6, 9, 34]

In [80]: num.sort(reverse=True)

In [81]: num
Out[81]: [34, 9, 6, 4, 2, 1, 1]

 练习:实现一个班级学生管理程序

[root@localhost python]# vim 21.py      
  1 print("="*50)
  2 print("学生名字管理系统".center(50))
  3 print("输入1:表示添加学生")
  4 print("输入2:查找学生名字")
  5 print("输入3:修改学生名字")
  6 print("输入4:删除学生")
  7 print("输入5:退出")
  8 
  9 stus=[]
 10 while True:
 11     operate=input("请输入你想要的操作:")
 12     if operate=="1":
 13         name=input("请输入添加的学生姓名:")
 14         stus.append(name.strip())
 15         print(stus)
 16 
 17     if operate=="2":
 18         name=input("请输入要查找的学生姓名:")
 19         if name not in stus:
 20             print("你查找的学生%s不存在"%name)
          continue
21 else: 22 print("你查找的学生%s存在"%name) 24 print(stus) 26 if operate=="3": 27 name=input("请输入你要修改的学生名字:") 28 if name not in stus: 29 print("你要修改的学生名字不存在") 30 continue 31 else: 32 stus[stus.index(name)]=input("请输入修改后的姓名:") 33 print(stus) 34 35 if operate=="4": 36 name=input("请输入要删除的学生姓名:") 37 if name not in stus: 38 print("你输入的学生%s不存在"%name) 39 continue 40 else: 41 stus.remove(name) 42 print(stus) 43 if operate=="5": 44 break [root@localhost python]# python3 21.py ================================================== 学生名字管理系统 输入1:表示添加学生 输入2:查找学生名字 输入3:修改学生名字 输入4:删除学生 输入5:退出 请输入你想要的操作:1 请输入添加的学生姓名:zyj ['zyj'] 请输入你想要的操作:sl 请输入你想要的操作:1 请输入添加的学生姓名:sl ['zyj', 'sl'] 请输入你想要的操作:1 请输入添加的学生姓名:lm ['zyj', 'sl', 'lm'] 请输入你想要的操作:2 请输入要查找的学生姓名:lm 你查找的学生lm存在 ['zyj', 'sl', 'lm'] 请输入你想要的操作:2 请输入要查找的学生姓名:liming 你查找的学生liming不存在 请输入你想要的操作:3 请输入你要修改的学生名字:zyj 请输入修改后的姓名:zhaoyujiao ['zhaoyujiao', 'sl', 'lm'] 请输入你想要的操作:3 请输入你要修改的学生名字:sl 请输入修改后的姓名:songlin ['zhaoyujiao', 'songlin', 'lm'] 请输入你想要的操作:3 请输入你要修改的学生名字:liming 你要修改的学生名字不存在 请输入你想要的操作:4 请输入要删除的学生姓名:liming 你输入的学生liming不存在 请输入你想要的操作:4 请输入要删除的学生姓名:lm ['zhaoyujiao', 'songlin'] 请输入你想要的操作: 请输入你想要的操作: 请输入你想要的操作:5
原文地址:https://www.cnblogs.com/zhaoyujiao/p/9010492.html