python - 如何将列表中的值与嵌套列表的第一个值进行比较并返回嵌套列表结果?

标签 python compare nested-lists

我有以下两个列表。

列表 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"

知道如何做到这一点吗? 谢谢

最佳答案

试一试:

a = ('a', 'b', 'h', 'g', 'e', 't', 'w', 'x')
b = (('a', 'yellow'), ('h', 'green'), ('t', 'red'), ('w', 'teal'))
B = dict(b)
print [(x, B.get(x, 'null')) for x in a]

关于python - 如何将列表中的值与嵌套列表的第一个值进行比较并返回嵌套列表结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5499286/

相关文章:

python - 如何解决 "ERROR: Failed building wheel for doom-py"

c# - 在 C# 中比较两个任意长度的列表或数组;顺序很重要

time - 根据时间改变背景 - Objective C

javascript - jQuery 比较两个 DOM 对象?

amazon-web-services - 如何在 terraform 资源中的 zipmap 和列表上创建嵌套 for 循环

python - 如何从图像中分离出人脸?

用于后缀的 Python 过滤器

html - 嵌套的垂直和水平列表?

python - 如何使用代码中的注释从 python 生成 swagger 规范文件 (json)?

r - 有没有一种快速的方法来修改 R 中嵌套列表的一致组件?