Python中的列表-List
用方括号创建列表,列表中的元素是有序的,可变的,第一个元素的索引为0
1#利用函数len()获取列表的长度
2test=["a","b","c"]
3print(len(test))#结果为3
利用索引访问列表元素,第一个元素索引为0,负向索引最后一个为-1,从A到B含头不含尾
1#利用索引访问列表元素
2test=["a","b","c"]
3print(test[0])#结果为a
4print(test[-1])#结果为c
5print(test[1:2])#结果为b
6print(test[-3:-2])#结果为a
用in检查某个元素是否位于列表中
1test=["a","b","c"]
2if "b" in test:
3 print("yes")#结果为yes
改变、增加、删减列表元素
1test=["a","b","c"]
2#改变指定位置一个元素
3test[1]="M"
4print(test)#结果为["a","M","c"]
5#改变指定位置多个元素
6test[1:3]=["L","S"]
7print(test)#结果为["a","L","S"]
8#用新列表替代旧列表部分元素
9new=["X","Y"]
10test[1:3]=new
11print(test)#结果为["a","X","Y"]
12single=["M"]
13test[1:3]=single
14print(test)#结果为["a","M"]
15#在指定位置插入新元素
16test=["A","b","c"]
17test.insert(1,"S")
18print(test)#结果为["A","S","b","c"],注意无法直接运行print(test.insert(1,"S"))
19#用append()在列表末尾添加单个元素,单个元素可以时一个字符串,也可以时一个列表
20test=["A","b","c"]
21test.append("apple")
22print(test)#结果为["A","b","c","apple"]
23#用extend()扩展列表
24test=["A","b","c"]
25new=["apple","orange"]
26new1=("apple","orange")
27test.extend(new)
28print(test)#结果为["A","b","c","apple","orange"]
29test.extend(new1)
30print(test)#结果为["A","b","c","apple","orange","apple","orange"]
31#用remove()删除特定元素
32test=["A","b","c"]
33test.remove("b")
34print(test)#结果为["A","c"]
35#用pop()删除指定位置元素,默认为最后一个
36test=["A","b","c"]
37test.pop()
38print(test)#结果为["A","b"]
39test=["A","b","c"]
40test.pop(1)
41print(test)#结果为["A","c"]
42#用关键词del删除指定索引的元素
43test=["A","b","c"]
44del test[0]
45print(test)#结果为["b","c"]
46#用clear()清空列表
47test=['A','b','c']
48test.clear()
49print(test)#结果为[]
遍历列表中的所有元素
1#利用for循环遍历列表所有元素
2test=["a","b","c"]
3for i in test:
4 print(i)#结果为a b c
5
6#利用for循环和range()通过访问index的形式访问列表中所有元素
7test=['a','b','c']
8for i in range(len(test)):
9 print(test[i])#结果为a b c
10#利用while循环遍历
11test=['a','b','c']
12i=0
13while i < len(test):
14 print(test[i])#结果为a b c
15 i=i+1
16#利用python的列表推导式遍历整个列表
17test=['a','b','c']
18[print(x) for x in test]
python中的列表推导式,利用已有的列表生成新的列表
1#List Comprehension格式
2newlist=[expression for item in iterable if condition == True]
3
4fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
5newlist=[x for x in fruits if x != "apple"]
6print(newlist)#结果为[ "banana", "cherry", "kiwi", "mango"]
7newlist=[x for x in fruits]
8print(newlist)#结果为["apple", "banana", "cherry", "kiwi", "mango"]
9newlist=[i.upper() for i in fruits if "a" in i]
10print(newlist)#结果为["APPLE","BANANA","MANGO"]
11newlist=["hello" for i in fruits]
12print(newlist)#结果为['hello','hello','hello','hello','hello']
13newlist = [x if x != "banana" else "orange" for x in fruits]
14print(newlist)#结果为['apple', 'orange', 'cherry', 'kiwi', 'mango']
对list进行排序
1#利用sort()对list进行排序
2test=['a','z','f']
3test.sort()
4print(test)#结果为['a','f','z']
5test=[0,4,2,7,9,5]
6test.sort()
7print(test)#结果为[0,2,4,5,7,9]
8#反向排序
9test=['a','z','f']
10test.sort(reverse=True)
11print(test)#结果为['z','f','a']
12#默认排序区分大小写,即大写和大写比较,小写和小写比较,大写在前,小写在后,可用str.lower
13test=['apple','Banana','Orange',"lemon"]
14test.sort()
15print(test)#结果为['Banana','Orange','apple','lemon']
16test.sort(key=str.lower)
17print(test)#结果为['apple','Banana','lemon','Orange']
18
19#用reverse()反转列表
20test=['apple','Banana','Orange',"lemon"]
21test.reverse()
22print(test)#结果为['lemon','Orange','Banana','apple']
复制列表
1test=['apple','Banana','Orange',"lemon"]
2new=test#相当于做了一个软链接,test改变时,new也会改变
3print(new)#结果为['apple','Banana','Orange',"lemon"]
4test[0]="APPLE"
5print(test)#结果为['APPLE','Banana','Orange',"lemon"]
6print(new)#结果为['APPLE','Banana','Orange',"lemon"]
7
8#利用copy()复制列表,而非建立软链接
9new=test.copy()
10print(new)#结果为['APPLE','Banana','Orange',"lemon"]
11test[0]="Cherry"
12print(test)#结果为['Cherry','Banana','Orange',"lemon"]
13print(new)#结果为['APPLE','Banana','Orange',"lemon"]
14
15#利用list()将旧列表的值赋给新列表
16new=list(test)
17print(new)#结果为['Cherry','Banana','Orange',"lemon"]
合并列表
1#直接用+连接多个列表
2list1=[1,2,3]
3list2=['a','b','c']
4list3=list1+list2
5print(list3)#结果为[1,2,3,'a','b','c']
6#利用for循环和append()将列表2的元素逐个添加到列表1中
7for i in list2:
8 list1.append(i)
9print(list1)#结果为[1,2,3,'a','b','c']
列表中的其他方法
1#用count()对列表中特定元素进行计数
2fruits = ['apple', 'banana', 'cherry','apple']
3print(fruits.count("cherry"))#结果为1
4print(fruits.count("apple"))#结果为2
5#用index()获取特定元素的位置
6print(fruits.index('cherry'))#结果为2
7print(fruits.index('apple'))#结果为0,如果有多个,则只返回第一次出现的位置
原文始发于微信公众号(BioInfo):W3Scchools-Python基础2
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/238192.html