如果在python中给出一些元组索引,如何获取子元组列表?

时间:2022-09-28 14:33:12

There is a list of tuple like

有一个元组列表

l = [(1, 2, 'a', 'b'), (3, 4, 'c', 'd'), (5, 6, 'e', 'f')]

I can use

我可以用

[(i[0], i[2], i[3]) for i in l]

to get the result

得到结果

[(1, 'a', 'b'), (3, 'c', 'd'), (5, 'e', 'f')]

But if given a variable list such as [0, 2, 3], how to get the similar result?

但如果给出一个变量列表,如[0,2,3],如何得到类似的结果?

2 个解决方案

#1


8  

Use operator.itemgetter, like this

像这样使用operator.itemgetter

>>> from operator import itemgetter
>>> getter = itemgetter(0, 2, 3)
>>> [getter(item) for item in l]
[(1, 'a', 'b'), (3, 'c', 'd'), (5, 'e', 'f')]

If you have a list of indices, then you can unpack them to the itemgetter, like this

如果你有一个索引列表,那么你可以将它们解压缩到itemgetter,就像这样

>>> getter = itemgetter(*[0, 2, 3])
>>> [getter(item) for item in l]
[(1, 'a', 'b'), (3, 'c', 'd'), (5, 'e', 'f')]

#2


5  

You could use a generator expression and tuple() to pull out specific indices:

您可以使用生成器表达式和tuple()来提取特定索引:

[tuple(t[i] for i in indices) for t in l]

or you can use a operator.itemgetter() object to create a callable that does the same:

或者您可以使用operator.itemgetter()对象来创建执行相同操作的可调用对象:

from operator import itemgetter

getindices = itemgetter(*indices)
[getindices(t) for t in l]

where indices is your list of indexes. This works because operator.itemgetter() just happens to return a tuple object when retrieving multiple indexes.

其中indices是您的索引列表。这是有效的,因为operator.itemgetter()恰好在检索多个索引时返回一个元组对象。

Demo:

演示:

>>> l = [(1, 2, 'a', 'b'), (3, 4, 'c','d'), (5, 6, 'e','f')]
>>> indices = [0, 1, 2]
>>> [tuple(t[i] for i in indices) for t in l]
[(1, 2, 'a'), (3, 4, 'c'), (5, 6, 'e')]
>>> getindices = itemgetter(*indices)
>>> from operator import itemgetter
>>> [getindices(t) for t in l]
[(1, 2, 'a'), (3, 4, 'c'), (5, 6, 'e')]

#1


8  

Use operator.itemgetter, like this

像这样使用operator.itemgetter

>>> from operator import itemgetter
>>> getter = itemgetter(0, 2, 3)
>>> [getter(item) for item in l]
[(1, 'a', 'b'), (3, 'c', 'd'), (5, 'e', 'f')]

If you have a list of indices, then you can unpack them to the itemgetter, like this

如果你有一个索引列表,那么你可以将它们解压缩到itemgetter,就像这样

>>> getter = itemgetter(*[0, 2, 3])
>>> [getter(item) for item in l]
[(1, 'a', 'b'), (3, 'c', 'd'), (5, 'e', 'f')]

#2


5  

You could use a generator expression and tuple() to pull out specific indices:

您可以使用生成器表达式和tuple()来提取特定索引:

[tuple(t[i] for i in indices) for t in l]

or you can use a operator.itemgetter() object to create a callable that does the same:

或者您可以使用operator.itemgetter()对象来创建执行相同操作的可调用对象:

from operator import itemgetter

getindices = itemgetter(*indices)
[getindices(t) for t in l]

where indices is your list of indexes. This works because operator.itemgetter() just happens to return a tuple object when retrieving multiple indexes.

其中indices是您的索引列表。这是有效的,因为operator.itemgetter()恰好在检索多个索引时返回一个元组对象。

Demo:

演示:

>>> l = [(1, 2, 'a', 'b'), (3, 4, 'c','d'), (5, 6, 'e','f')]
>>> indices = [0, 1, 2]
>>> [tuple(t[i] for i in indices) for t in l]
[(1, 2, 'a'), (3, 4, 'c'), (5, 6, 'e')]
>>> getindices = itemgetter(*indices)
>>> from operator import itemgetter
>>> [getindices(t) for t in l]
[(1, 2, 'a'), (3, 4, 'c'), (5, 6, 'e')]