python
for slogan in list1 for name in list2 if slogan == name]谁能帮我解释一下谢谢
先举几个列表推导式的例子吧(假设已经给出了列表 ls1,ls2):
temp =
最简单的列表推导式:将 遍历 ls1中的元素 依次赋值给 x 加入列表中去
可以分解成:
temp = []
for x in ls1:
temp.append(x)
temp =
将遍历ls1中的元素,将符合条件的元素加的列表中
可以分解成这样:temp = []
for x in ls1:
if <条件>:
temp.append(x)
temp =
将 x 遍历 ls1 中的元素,然后 i 遍历 ls2 的元素 将符合条件后的 x+i 的结果依次加入列表中
可以分解成这样:temp = []
for x in ls1:
for i in ls2:
if <条件>:
temp.append(x+i)
list3= for slogan in list1 for name in list2 if slogan == name]
所以你的这题可以分解成这样:
list3=[]
for slogan in list1:
for name in list2:
if slogan==name:
list3.append(name + ':'+ slogan)
slogan是个函数吗
超神奇葩天才 发表于 2020-6-11 10:56
slogan是个函数吗
不是的只是个普普通通的变量名,你可以随意更改,只是良好的命名方式可以大大提示代码的可读性 谢谢
页:
[1]