如何将列表中的值与嵌套列表的第一个值进行比较并返回嵌套列表结果?
我有以下两个列表。
列表 1
(a,b,h,g,e,t,w,x)
列表二
((a,yellow),(h,green),(t,red),(w,teal))
我想返回以下内容
((a,yellow),(b,null),(h,green),(e,null),(t,red),(w,teal),(x,null))
for x in List_1:
for y in list_2:
if x == y
print y
else print x, "null"
您知道如何执行此操作吗? 谢谢
I have the following two lists.
List 1
(a,b,h,g,e,t,w,x)
List two
((a,yellow),(h,green),(t,red),(w,teal))
I want to return the following
((a,yellow),(b,null),(h,green),(e,null),(t,red),(w,teal),(x,null))
for x in List_1:
for y in list_2:
if x == y
print y
else print x, "null"
Any idea on how to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
列表理解中的简短 Python 列表理解:
更长的版本:
A short pythonic list comprehension within a list comprehension:
A longer version:
试一试:
Give this a go:
你的逻辑是正确的。您唯一需要的是形成一个列表,而不是直接打印结果。
如果您坚持使用嵌套循环(这是一项作业,对吧?),您需要这样的东西:
Your logic is correct. The only thing you need is to form a list, not to print results outright.
If you insist on nested loops (it's a homework, right?), you need something like this:
另一种方法
Another way to do it