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

时间:2015-03-09 06:34:20

标签: python list 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))]

5 个答案:

答案 0 :(得分:2)

使用简单的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)

答案 1 :(得分:1)

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))]

答案 2 :(得分:1)

似乎只想在相应的索引0值为2618

的情况下才获取位于索引1的元组元素
>>> 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)]

答案 3 :(得分:1)

或者您可以使用字典:

>>> 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]
编辑:嗯,我已经更新,用字典来解决你的问题。

答案 4 :(得分:1)

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

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

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)