- 什么是列表?
列表由一系列按特定顺序排列的元素组成。你可以创建包含字母表中所有字母、数字0-9或所有家庭成员姓名的列表;也可以将任何东西加入列表中,其中的元素之间可以没有任何关系。鉴于列表通常包含多个元素,给列表指定一个表示复数的名称(如letters、digits或names)是个不错的主意。
在Python中,用方括号([])来表示列表,并用逗号来分隔其中的元素。
若想直接生成一个列表,可以使用列表推导式。下面介绍几种简单的推导方式,可根据自己的需求自行选用哦!
- 方法一(简单):
1 '''将原列表中的数字加一并打印出来'''
2 l = [1,2,3,4,5]
3
4 for index,i in enumerate(l):
5 l[index] += 1
6 print(l)
7
8 #输出结果:[2, 3, 4, 5, 6]
上述方法中的enumerate方法,详细用法请点击Enumerate链接,具体在写在类型转换模块中。
- 方法二(一般):
1 1 '''将原列表中的数字加一并打印出来'''
2 2 l = [1,2,3,4,5]
3 3 a = list(map(lambda x:x+1,l))
4 4 print(a)
5 5
6 6 #输出结果: [2, 3, 4, 5, 6]
上述方法中的lambda函数,详细用法请点击Lambda
- 方法三(高级):
'''将原列表中的数字加一并打印出来'''
l = [i+1 for i in range(1,6)]
print(l)
#输出结果:[2, 3, 4, 5, 6]
- 列表常用操作:
1.索引
#索引:
'''
从列表左边开始,第一个元素下标记为0,即index[0]
第二个元素下标记为1,即index[1],依次类推;
若要索引列表末尾的字符串,则最后一个元素的下标记为-1,即index[-1]
倒数第二个元素下标记为-2,即index[-2],依次类推。
'''
name = ["ka","fds","ll","df","dfg","ll"]
print(name)
print(name[0],name[1],name[3])
print(name[-1])
输出结果为:
['ka', 'fds', 'll', 'df', 'dfg', 'll']
ka fds df
ll
2.切片:
切片是根据元素下标来获取的,与索引类似!
[0:i]一个冒号表示从第一个元素开始(0是可以省略的),到第i个元素结束,但不包括第i个元素,即顾首不顾尾。
[1::3]两个冒号表示跳转,即从第二个元素开始,到第四个元素结束,跳过中间元素。
#切片:
name = ["ka","fds","ll","df","dfg","ll"]
print(name)
print(name[0:2]) #切片必须从左至右
print(name[0::3]) #[::]表示跳过中间元素
输出结果:
['ka', 'fds', 'll', 'df', 'dfg', 'll']
['ka', 'fds']
['ka', 'df']
3.插入
+ :连字符“+”号,只能连接同类型的数据类型,比如列表+列表;通过“+”号只能将元素添加在末尾
append():直接添加到末尾
insert(index[i],x):将元素x添加到下标为i的位置
"+"
name1 = ["ka","fds","ll","df","dfg","ll"]
name2 = ["you","are","a","pig"]
name = name1 + name2
print(name)
输出结果:
['ka', 'fds', 'll', 'df', 'dfg', 'll', 'you', 'are', 'a', 'pig']
"append"
name = ["ka","fds","ll","df","dfg","ll"]
name.append("you")
name.append("are")
name.append("a")
name.append("pig")
print(name)
输出结果:
['ka', 'fds', 'll', 'df', 'dfg', 'll', 'you', 'are', 'a', 'pig']
"insert"
name = ["ka","fds","ll","df","dfg","ll"]
name.insert(2,"HAHAHA")
name.insert(4,"WWW")
print(name)
输出结果:
['ka', 'fds', 'HAHAHA', 'll', 'WWW', 'df', 'dfg', 'll']
4.修改
在 [] 中输入元素下标去修改原列表中的元素
name = ["ka","fds","ll","df","dfg","ll"]
print(name)
name[2] = "HAHAHA"
name[-1] = "88888"
print(name)
输出结果:
['ka', 'fds', 'll', 'df', 'dfg', 'll']
['ka', 'fds', 'HAHAHA', 'df', 'dfg', '88888']
5.删除:
remove():直接删除,没有返回值
pop():抛出,删除后有返回值
del():全局删除方法,不是列表所特有
6.查找元素:
index():当原列表中有多个相同元素时,index()只会查找出从左至右第一个元素的下标
name = ["ka","fds","ll","ll","df","dfg","ll"]
print(name)
a = name.index("ll")
print(a)
输出结果:
2
7.count():输出元素个数
name = ["ka","fas","dfe","ka","dere","ka"]
print(name.count("ka"))
输出结果:
3
8.clear():清除(清空)
name = ["df","D","dfd","dfe","gr","df","hgr"]
name.clear()
print(name)
输出结果:
[]
9.reverse():反转顺序
name = ["A","B","C","D","E","F"]
name.reverse()
print(name)
输出结果:
['F', 'E', 'D', 'C', 'B', 'A']
10.extend():合并(添加)
extend()方法比较有趣:它会把"QQ"拆分两个元素并添加在原列表中
name = ["ka","fds","ll","df","dfg","ll"]
name.extend("QQ")
print(name)
输出结果:
["ka","fds","ll","df","dfg","ll","Q","Q"]
练习:
1.去重(去除重复的元素)
1 n = ["a","b","c","d","a","c","u","o","b","p","b"]
2 n1 = []
3 print(n)
4 for i in n:
5 if i not in n1:
6 n1.append(i)
7 else:
8 continue
9 print(n1)
10 print(n1)
2.多级菜单
1 n = [["主菜单",["模块",[8,9,[1,3,5]]]],["退出",[2,4]]]
2 for i in range(1):
3 print("1.",n[0][0])
4 print("2.",n[0][1][0])
5 print(n[0][1][1][0],n[0][1][1][2],n[0][1][1][2][0])
6 for i in range(1):
7 print("3.",n[1][0])
8 print(n[1][1][0],n[1][1][1])