关于课后作业 第012讲6题
>>> list1 = ['1.Jost do it','2.一切皆有可能','3.让编程改变世界','4.Impossible is nothing']>>> list2 = ['4.阿迪达斯','2.李宁','3.鱼C工作室','1.耐克']
>>> list3 = for slogan in list1 for name in list2 if slogan == name]
>>> for i in list3:
print(i)
1.耐克:Jost do it
2.李宁:一切皆有可能
3.鱼C工作室:让编程改变世界
4.阿迪达斯:Impossible is nothing
>>>
其中list3这一串我愣是没看懂有大佬可以帮我解读一下吗 本帖最后由 Twilight6 于 2020-5-22 18:17 编辑
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)
for slogan in list1 for name in list2 if slogan == name]
就是
for slogan in list1:
for name in list2:
循环过程中只要if slogan == name 条件成立 就把name + ':' + slogan 加入列表中去
页:
[1]