后面接的if操作是可选项。
lst = [i * i for i in range(1,10) if i%2==0] #生成一个新列表 print(lst) # [4, 16, 36, 64]
同理字典推导式和集合推导式也可以if选项。
# 字典推导式1: items = ["Fruits","Books","Othes"] prices=[23,45,73,56,78] dic = {item:price for item,price in zip(items,prices)} # {'Fruits': 23, 'Books': 45, 'Othes': 73} print(dic) # 字典推导式2: testDict= {i: i *i for i in range(10)} print(testDict) # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
s= {i * i for i in range(1,10)} print(s)
输出:
{64, 1, 4, 36, 9, 16, 49, 81, 25}
当while和for条件正常结束后,将会执行else操作,如果是使用break强制跳出,则不会执行else
。
while举例:
item = 0 while item < 3: item+=1 print(item) else: print("正常结束!")
for举例:
lst = [1,2,3] for item in lst: print(item) else: print("正常结束!")
以上两个都输出:
1 2 3 正常结束!
while举例:
item = 0 while item < 3: item += 1 print(item) if item == 2: break else: print("正常结束!")
for举例:
lst = [1,2,3] for item in lst: print(item) if item == 2: break else: print("正常结束!")
以上两个都输出:
1 2
x,y,z= 10,20,30 print(x,y,z) # 10 20 30 x,y,z= y,z,x print(x,y,z) # 20 30 10
if m in [1,3,5,7]:
等价于:
if m==1 or m==3 or m==5 or m==7:
在原列表基础上排序
。lst = [9,2,5,6,8] # 内建函数方法: print(sorted(lst)) # [2, 5, 6, 8, 9] print(lst) # [9, 2, 5, 6, 8] # 列表方法: lst.sort() print(lst) # [2, 5, 6, 8, 9]
这种方式,元素个数与列表长度应该严格相同,不然会报错
。
lst= [1,2,3] x,y,z= lst print(x,y,z)
输出:
1 2 3
ans = 3 if 1 > 2 else 4 #if语句为true输出自己,否则输出else print(ans) # 4
test= [1,2,3,4,2,2,3,1,4,4,4,4] print(max(set(test),key=test.count))
输出:
4
ans = 3 if 4 < ans >2: print(ans) else: print("else")
输出:
else
lst_1 = ["11", "22"] lst_2 = ["33", "44"] for a, b in zip(lst_1, lst_2): print(a + " && " + b)
输出:
11 && 33 22 && 44
names = ['John', 'Amy', 'Jack'] scores = [98, 100, 85] # 分数和名字是一一对应的 #按照names排序 data = list(zip(names, scores)) data.sort() print(data) # [('Amy', 100), ('Jack', 85), ('John', 98)] ,names和scores列表不会发生改变 #按照分数进行排序 data = list(zip(scores, names)) data.sort() print(data) # [(85, 'Jack'), (98, 'John'), (100, 'Amy')],names和scores列表不会发生改变
t1= (1,2,3) t2= (10,20,30) dic = dict(zip(t1,t2)) print(dic)
输出:
{1: 10, 2: 20, 3: 30}
teams = [1,2,3,4] for index, team in enumerate(teams): print(index, team)
输出:
0 1 1 2 2 3 3 4
lst = [1]*3 print(lst)
输出:
[1, 1, 1]
lst = [1,2,3,4,5] # 反转方式1: lst = lst[::-1] print(lst) # [5, 4, 3, 2, 1] # 反转方式2:内置函数 lst.reverse() print(lst) # [1, 2, 3, 4, 5] # 反转方法3:列表方法 #reversed出来的结果是反向的迭代器 lst1 = reversed(lst) print(type(lst1)) # <class 'list_reverseiterator'> lst = list(lst1) #内置方法不会影响原始值,所以要进行赋值操作 print(lst) # [5, 4, 3, 2, 1]
my_dict = { 'sum':lambda x,y:x+y, 'subtract':lambda x,y:x-y } print(my_dict['sum'](9,3)) # 12 print(my_dict['subtract'](9,3)) # 6
data = [1,4,5,6,7,4,334,5,23,1,3,4,4] # 列表中出现数字出现的次数 d = dict.fromkeys(data, 0) #构建字典,键为:data,值为0 print(d) # {1: 0, 4: 0, 5: 0, 6: 0, 7: 0, 334: 0, 23: 0, 3: 0} for v in data: d[v] += 1 print(d) # {1: 2, 4: 4, 5: 2, 6: 1, 7: 1, 334: 1, 23: 1, 3: 1}
from collections import Counter c = Counter('hello world') dic = dict(c) print(c) print(dic) #统计出现频率最高的前n个数 print(c.most_common(2)) # [('l', 3), ('o', 2)]
输出:
Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1}) {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1} [('l', 3), ('o', 2)]
data = {'user': 1, 'name': 'Max', 'three': 4} # 字典可以通过[]进行赋新值,但是不能访问没有的键值对 data['admin'] = data.get('admin', 6) + 4 print(data['admin']) # 10 #不可以访问没有的元素 print(data['new']) #不可以, 报错!!!