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

发布于 2024-10-28 18:23:30 字数 399 浏览 11 评论 0原文

我有以下两个列表。

列表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自此以后,行同陌路 2024-11-04 18:23:31

列表理解中的简短 Python 列表理解:

[(i, ([j[1] for j in list2 if j[0] == i] or ['null'])[0]) for i in list1]

更长的版本:

def get_nested(list1, list2):
    d = dict(list2)
    for i in list1:
        yield (i, i in d and d[i] or 'null')
print tuple(get_nested(list1, list2))

A short pythonic list comprehension within a list comprehension:

[(i, ([j[1] for j in list2 if j[0] == i] or ['null'])[0]) for i in list1]

A longer version:

def get_nested(list1, list2):
    d = dict(list2)
    for i in list1:
        yield (i, i in d and d[i] or 'null')
print tuple(get_nested(list1, list2))
黒涩兲箜 2024-11-04 18:23:30

试一试:

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]

Give this a go:

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]
じ违心 2024-11-04 18:23:30

你的逻辑是正确的。您唯一需要的是形成一个列表,而不是直接打印结果。

如果您坚持使用嵌套循环(这是一项作业,对吧?),您需要这样的东西:

list1 = ["a", "b", "h", "g", "e", "t", "w", "x"]
list2 = [("a", "yellow"), ("h", "green"), ("t", "red"), ("w", "teal")]

result = [] # an empty list
for letter1 in list1:
  found_letter = False # not yet found
  for (letter2, color) in list2:
    if letter1 == letter2:
      result.append((letter2, color))
      found_letter = True # mark the fact that we found the letter with a color
  if not found_letter:
      result.append((letter1, 'null'))

print result

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:

list1 = ["a", "b", "h", "g", "e", "t", "w", "x"]
list2 = [("a", "yellow"), ("h", "green"), ("t", "red"), ("w", "teal")]

result = [] # an empty list
for letter1 in list1:
  found_letter = False # not yet found
  for (letter2, color) in list2:
    if letter1 == letter2:
      result.append((letter2, color))
      found_letter = True # mark the fact that we found the letter with a color
  if not found_letter:
      result.append((letter1, 'null'))

print result
只想待在家 2024-11-04 18:23:30

另一种方法

list1 = ["a", "b", "h", "g", "e", "t", "w", "x"]
list2 = [("a", "yellow"), ("h", "green"), ("t", "red"), ("w", "teal")]
print dict(((x, "null") for x in list1), **dict(list2)).items()

Another way to do it

list1 = ["a", "b", "h", "g", "e", "t", "w", "x"]
list2 = [("a", "yellow"), ("h", "green"), ("t", "red"), ("w", "teal")]
print dict(((x, "null") for x in list1), **dict(list2)).items()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文