在元组中搜索组合

时间:2020-06-05 01:39:15

标签: python list tuples

Traceback (most recent call last):
  File "/home/yapjiahong/Coding/python/completed/keyboard/compare.py", line 26, in <module>
    keyboard.write(word_list_PG[random_word])
  File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 854, in write
    scan_code, modifiers = next(iter(entries))
StopIteration

如何访问a = [[3, (1, 2, 3, 4, 5)], [3, (5, 4, 3, 2, 1)]] b = [[3, (18, 24, 21, 2, 3)], [3, (3, 4, 76, 7, 8)]] (1, 2, 3, 4, 5)(5, 4, 3, 2, 1)(18, 24, 21, 2, 3)

我必须在其他列表中搜索此元素。

例如:(3, 4, 76, 7, 8)是否在列表(3, 4, 76, 7, 8)中,如果是,此元素的具体编号是什么?

(在这种情况下[3,(3,4,76,7,8)])

谢谢。

1 个答案:

答案 0 :(得分:0)

>>> a = [[3, (1, 2, 3, 4, 5)], [3, (5, 4, 3, 2, 1)]]
>>> b = [[3, (18, 24, 21, 2, 3)], [3, (3, 4, 76, 7, 8)]]
>>> [x for x in a if (3, 4, 76, 7, 8) == x[1]]
[]
>>> [x for x in b if (3, 4, 76, 7, 8) == x[1]]
[[3, (3, 4, 76, 7, 8)]]
>>> [x for x in a + b if (3, 4, 76, 7, 8) == x[1]]
[[3, (3, 4, 76, 7, 8)]]