列表
- >>> member = ["苹果","香蕉","草莓","鸭梨"]
- >>> member
- ['苹果', '香蕉', '草莓', '鸭梨']
- >>> num = [1,2,3,4]
- >>> num
- [1, 2, 3, 4]
- >>> mix = ["苹果",True,[1,2,4]] #混合列表
- >>> mix
- ['苹果', True, [1, 2, 4]]
- >>> mix = [] #空列表
- >>> mix
- []
- append(元素):添加元素,只能是一个参数。
- >>> member = ["苹果","香蕉","草莓","鸭梨"] #append(元素):添加元素,只能是一个参数
- >>> member.append("哈密瓜")
- >>> member
- ['苹果', '香蕉', '草莓', '鸭梨', '哈密瓜']
- >>> member.append("菠萝","水蜜桃")
- Traceback (most recent call last):
- File "<pyshell#13>", line 1, in <module>
- member.append("菠萝","水蜜桃")
- TypeError: append() takes exactly one argument (2 given) #两个参数会报错。
- >>> member.extend("菠萝","水蜜桃") #extend(列表):扩展一个列表,注意是列表,不是元素。
- Traceback (most recent call last):
- File "<pyshell#15>", line 1, in <module>
- member.extend("菠萝","水蜜桃")
- TypeError: extend() takes exactly one argument (2 given) #扩展元素会报错
- >>> member.extend(["菠萝","水蜜桃"]) #扩展列表
- >>> member
- ['苹果', '香蕉', '草莓', '鸭梨', '哈密瓜', '菠萝', '水蜜桃']
- >>> member.insert(0,"牡丹") #insert(插入位置,元素):在指定位置添加一个元素
- >>> member
- ['牡丹', '苹果', '香蕉', '草莓', '鸭梨', '哈密瓜', '菠萝', '水蜜桃']
- >>> member = ["苹果","香蕉","草莓","鸭梨"]
- >>> member[0] #获取指定位置的元素
- '苹果'
- >>> member.remove("香蕉") #删除元素
- >>> member
- ['苹果', '草莓', '鸭梨']
- >>> del member[1]
- >>> member
- ['苹果', '鸭梨'] #删除元素
- >>> del member[] #删除列表
- SyntaxError: invalid syntax
- >>> del member
- >>> member
- Traceback (most recent call last):
- File "<pyshell#31>", line 1, in <module>
- member
- NameError: name 'member' is not defined
- >>> member = ["苹果","香蕉","草莓","鸭梨"]
- >>> member.pop() #pop(索引值):删除指定位置的元素,并返回该元素。如果pop不给值就删除并返回最后一个元素,
- '鸭梨'
- >>> member
- ['苹果', '香蕉', '草莓']
- >>> member = ["苹果","香蕉","草莓","鸭梨"]
- >>> member.pop(1)
- '香蕉'
- >>> member
- ['苹果', '草莓', '鸭梨']
- >>> member = ["苹果","香蕉","草莓","鸭梨"]
- >>> member[1:3] #列表分片:返回指定索引值之间的一个新列表。原列表不变。[1:3]:1,2
- ['香蕉', '草莓']
- >>> member
- ['苹果', '香蕉', '草莓', '鸭梨']
- >>> member[:3] #返回从0开始到索引值之间的一个新列表。
- ['苹果', '香蕉', '草莓']
- >>> member[1:] #返回从1开始到末尾的一个新列表。
- ['香蕉', '草莓', '鸭梨']
- >>> member2 = member[:] #拷贝一个列表,新增一个地址,对member操作,member不改变;如果member3 = member,它俩指向同一个地址,对member操作member3也改变
- >>> member2
- ['苹果', '香蕉', '草莓', '鸭梨']
- >>> list1 = [1,2,3] #列表比较
- >>> list2 = [2,3,4]
- >>> list1 > list2
- False
- >>> list1 = [123,456] #短路逻辑,比较完123>234为False,后面不用比较了
- >>> list2 = [234,123]
- >>> list1 > list2
- False
- >>> list3 = [123,456]
- >>> (list1 < list2) and (list1 == list3)
- True
- >>> list4 = list1 + list2 #列表相加
- >>> list4
- [123, 456, 234, 123]
- >>> list4 *2
- [123, 456, 234, 123, 123, 456, 234, 123]
- >>> list4 *=2
- >>> list4
- [123, 456, 234, 123, 123, 456, 234, 123]
- >>> 123 in list4 #成员操作符
- True
- >>> 245 <span style="color:#ff0000;">not in </span>list4
- True
- >>> list5 = [123,["小甲鱼","牡丹"],456] #成员操作符只能对一层进行操作。
- >>> "小甲鱼" in list5
- False
- >>> "小甲鱼" in list5[1]
- True
- >>> list5[1][1] #索引
- '牡丹'
- >>> list1 = [123,456,123,123]
- >>> list1.count(123) #count(元素):元素在列表里出现的次数
- 3
- >>> list1 = [123,456]
- >>> list1 *5
- [123, 456, 123, 456, 123, 456, 123, 456, 123, 456]
- >>> list1 *=5
- >>> list1
- [123, 456, 123, 456, 123, 456, 123, 456, 123, 456]
- >>> list1.index(123) #index(元素,起始索引地址,结束索引地址)
- 0 #默认从起始开始查找索引
- >>> list2 =[1,4,7,2,9,0,3]
- >>> list2.reverse() #列表翻转
- >>> list2
- [3, 0, 9, 2, 7, 4, 1]
- >>> list2.sort() #列表排序,默认列表从小到大
- >>> list2
- [0, 1, 2, 3, 4, 7, 9]
- >>> list2.sort(reverse=True) #再次翻转,从大到小排列
- >>> list2
- [9, 7, 4, 3, 2, 1, 0]
列表推导式(列表解析)
[有关A的表达式 for A in B]
- >>>> [i*i for i in range(10)]
- [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</span>
- list1 = []
- for i in range(10):
- list1.append(i*i)
- print(list1)
[
>>>> list1 = [(x,y)
for
x
in
range(
10
)
for
y
in
range(
10
)
if
x%
2
!=
0
if
y%
2
!=
0
]
- >>> list1
- [(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9), (6, 1), (6, 3), (6, 5), (6, 7), (6, 9), (8, 1), (8, 3), (8, 5), (8, 7), (8, 9)] #目前没看懂 (与下面代码等价)
- list1 =[]
- for x in range(10):
- for y in range(10):
- if x%2 ==0:
- if y%2 !=0:
- list1.append((x,y))
- print(list1)
- #一层一层for循环。x=0--->y=0--->不满足if,返回if的上一层循环--->x=0--->y=1--->…………继续循环