Python,从元组列表中选择特定元素

Python, to pick specific elements from a list of tuples

大家好…我有一个元组列表(图像中的颜色数。例如,那里有 6320(255、255、255)种颜色等)

我想要的是获取某些颜色的数量,比如“(159, 177, 205)”和“(223, 213, 196)”。 2和618是他们的对应关系

如何操作元组以便将它们挑出来(2 和 618)?

谢谢。

[(6320, (255, 255, 255)), (17, (249, 251, 253)), (9, (244, 245, 248)), (2, (159, 177, 205)), (3, (147, 145, 140)), (4, (241, 243, 246)), (138128, (240, 237, 229)), (1726, (237, 227, 208)), (7, (234, 237, 241)), (7298, (233, 229, 223)), (15, (212, 209, 202)), (3392, (229, 227, 216)), (3, (228, 235, 246)), (5962, (227, 223, 213)), (8, (226, 223, 216)), (2, (173, 194, 227)), (618, (223, 213, 196)), (3, (220, 225, 234)), (7, (217, 229, 247)), (6224, (216, 209, 200)), (2, (214, 227, 246)), (3, (213, 229, 252)), (7, (212, 223, 239)), (10, (211, 217, 226)), (9, (210, 225, 248)), (3279, (200, 223, 174)), (29, (197, 217, 247)), (7, (193, 217, 253))]

使用简单的 List comprehension,使用过滤条件和解包元组,像这样

>>> items_to_pick = [2, 618]
>>> [color for idx, color in list_of_tuples if idx in items_to_pick]
[(159, 177, 205), (173, 194, 227), (223, 213, 196), (214, 227, 246)]

但是,如果要过滤的项目数量很大,那么你可以将其转换为generator expression,只在需要时获取元素,就像这样

>>> my_filter = (color for idx, color in list_of_tuples if idx in items_to_pick)
>>> next(my_filter)
(159, 177, 205)
>>> next(my_filter)
(173, 194, 227)
>>> next(my_filter)
(223, 213, 196)
>>> next(my_filter)
(214, 227, 246)
>>> next(my_filter)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
StopIteration

或者,如果您只是要迭代过滤后的数据,您可以简单地迭代生成器,就像这样

>>> for item in (color for idx, color in list_of_tuples if idx in items_to_pick):
...     print(item)
...     
... 
(159, 177, 205)
(173, 194, 227)
(223, 213, 196)
(214, 227, 246)
x=[(6320, (255, 255, 255)), (17, (249, 251, 253)), (9, (244, 245, 248)), (2, (159, 177, 205)), (3, (147, 145, 140)), (4, (241, 243, 246)), (138128, (240, 237, 229)), (1726, (237, 227, 208)), (7, (234, 237, 241)), (7298, (233, 229, 223)), (15, (212, 209, 202)), (3392, (229, 227, 216)), (3, (228, 235, 246)), (5962, (227, 223, 213)), (8, (226, 223, 216)), (2, (173, 194, 227)), (618, (223, 213, 196)), (3, (220, 225, 234)), (7, (217, 229, 247)), (6224, (216, 209, 200)), (2, (214, 227, 246)), (3, (213, 229, 252)), (7, (212, 223, 239)), (10, (211, 217, 226)), (9, (210, 225, 248)), (3279, (200, 223, 174)), (29, (197, 217, 247)), (7, (193, 217, 253))]
print [k for k in x if k[0] in [2,618]]

这可以使用简单的列表理解来完成。

输出:[(2, (159, 177, 205)), (2, (173, 194, 227)), (618, (223, 213, 196)), (2, (214, 227, 246))]

似乎您只想获取位于索引 1 的元组元素,前提是相应的索引 0 值为 2618

>>> t = [(6320, (255, 255, 255)), (17, (249, 251, 253)), (9, (244, 245, 248)), (2, (159, 177, 205)), (3, (147, 145, 140)), (4, (241, 243, 246)), (138128, (240, 237, 229)), (1726, (237, 227, 208)), (7, (234, 237, 241)), (7298, (233, 229, 223)), (15, (212, 209, 202)), (3392, (229, 227, 216)), (3, (228, 235, 246)), (5962, (227, 223, 213)), (8, (226, 223, 216)), (2, (173, 194, 227)), (618, (223, 213, 196)), (3, (220, 225, 234)), (7, (217, 229, 247)), (6224, (216, 209, 200)), (2, (214, 227, 246)), (3, (213, 229, 252)), (7, (212, 223, 239)), (10, (211, 217, 226)), (9, (210, 225, 248)), (3279, (200, 223, 174)), (29, (197, 217, 247)), (7, (193, 217, 253))]
>>> [k[1] for k in t if k[0] in [2,618]]
[(159, 177, 205), (173, 194, 227), (223, 213, 196), (214, 227, 246)]

或者您可以使用字典:

>>> t = [(6320, (255, 255, 255)), (17, (249, 251, 253)), (9, (244, 245, 248))]
>>> dic = {x[0]:x[1] for x in t}
>>> print(dic)
{6320: (255, 255, 255), 17: (249, 251, 253), 9: (244, 245, 248)}
>>> list = [x for x, y in dic.items() if y == (249,251,253) or y == (1,1,1)]
>>> list
[17]

编辑:好的,我已经更新以解决您的字典问题。

为什么不使用字典而不是列表?

鉴于您的原始列表,我们将其称为元组:

tuples = [(6320, (255, 255, 255)), (17, (249, 251, 253)), (9, (244, 245, 248)), (2, (159, 177, 205)), (3, (147, 145, 140)), (4, (241, 243, 246)), (138128, (240, 237, 229)), (1726, (237, 227, 208)), (7, (234, 237, 241)), (7298, (233, 229, 223)), (15, (212, 209, 202)), (3392, (229, 227, 216)), (3, (228, 235, 246)), (5962, (227, 223, 213)), (8, (226, 223, 216)), (2, (173, 194, 227)), (618, (223, 213, 196)), (3, (220, 225, 234)), (7, (217, 229, 247)), (6224, (216, 209, 200)), (2, (214, 227, 246)), (3, (213, 229, 252)), (7, (212, 223, 239)), (10, (211, 217, 226)), (9, (210, 225, 248)), (3279, (200, 223, 174)), (29, (197, 217, 247)), (7, (193, 217, 253))]

您可以将其简洁地转换为字典,如下所示:

d = {k: v for k, v in tuples}

然后就可以访问618的元组了,例如如下:

print(d[618])
(223, 213, 196)