https://blog.csdn.net/weixin_40522801/article/details/106172470
只能遍历列表中的元素,不会获得索引值
list = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
for item in list:
print(item)
既遍历索引,又遍历元素
list = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
for index, item in enumerate(list):
print(index, item)
并行遍历列表
s = ["1", "2", "3", "4", "5"]
t = ["a", "b", "c", "d", "e"]
for x, y in zip(s, t):
print(x+"-"+y)
for i in range(10):
print(i)
school_dict = { i['name']: i['name2'] for i in jsn_arr }
https://blog.csdn.net/qq_34806812/article/details/82019683
L = [1, 2, 3, 4, 5, 6]
L = [x for x in L if x % 2 != 0]
#[1, 3, 5]
#带有for嵌套
[x + y for x in 'ab' for y in 'jk']
#['aj', 'ak', 'bj', 'bk']
#既有if语句又有for嵌套
[ expression for x in X [if condition]
for y in Y [if condition]
...
for n in N [if condition] ]
[(x, y) for x in range(5) if x % 2 == 0 for y in range(5) if y % 2 == 1]
#[(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]
#列表推导式生成矩阵