如何获取元组列表中的第一个元素?

问题:如何获取元组列表中的第一个元素?

我有一个像下面这样的列表,其中第一个元素是id,另一个是字符串:

[(1, u'abc'), (2, u'def')]

我只想从此元组列表创建ID列表,如下所示:

[1,2]

我将使用此列表,__in因此它必须是整数值的列表。

I have a list like below where the first element is the id and the other is a string:

[(1, u'abc'), (2, u'def')]

I want to create a list of ids only from this list of tuples as below:

[1,2]

I’ll use this list in __in so it needs to be a list of integer values.


回答 0

>>> a = [(1, u'abc'), (2, u'def')]
>>> [i[0] for i in a]
[1, 2]
>>> a = [(1, u'abc'), (2, u'def')]
>>> [i[0] for i in a]
[1, 2]

回答 1

使用zip函数解耦元素:

>>> inpt = [(1, u'abc'), (2, u'def')]
>>> unzipped = zip(*inpt)
>>> print unzipped
[(1, 2), (u'abc', u'def')]
>>> print list(unzipped[0])
[1, 2]

编辑(@BradSolomon):上面的代码适用于Python 2.x,其中zip返回列表。

在Python 3.x中,zip返回一个迭代器,以下等效于上述内容:

>>> print(list(list(zip(*inpt))[0]))
[1, 2]

Use the zip function to decouple elements:

>>> inpt = [(1, u'abc'), (2, u'def')]
>>> unzipped = zip(*inpt)
>>> print unzipped
[(1, 2), (u'abc', u'def')]
>>> print list(unzipped[0])
[1, 2]

Edit (@BradSolomon): The above works for Python 2.x, where zip returns a list.

In Python 3.x, zip returns an iterator and the following is equivalent to the above:

>>> print(list(list(zip(*inpt))[0]))
[1, 2]

回答 2

你的意思是这样吗?

new_list = [ seq[0] for seq in yourlist ]

您实际拥有的是tuple对象列表,而不是集合列表(正如您的原始问题所暗示的)。如果实际上是集合的列表,则没有第一个元素,因为集合没有顺序。

在这里,我创建了一个平面列表,因为通常来说,这似乎比创建1个元素元组的列表更有用。但是,只需替换seq[0]为,就可以轻松创建1个元素元组的列表(seq[0],)

do you mean something like this?

new_list = [ seq[0] for seq in yourlist ]

What you actually have is a list of tuple objects, not a list of sets (as your original question implied). If it is actually a list of sets, then there is no first element because sets have no order.

Here I’ve created a flat list because generally that seems more useful than creating a list of 1 element tuples. However, you can easily create a list of 1 element tuples by just replacing seq[0] with (seq[0],).


回答 3

您可以使用“元组拆包”:

>>> my_list = [(1, 'abc'), (2, 'def')]
>>> my_ids = [idx for idx, val in my_list]
>>> my_ids
[1, 2]

在迭代时,将每个元组解压缩,并将其值设置为变量idxval

>>> x = (1, 'abc')
>>> idx, val = x
>>> idx
1
>>> val
'abc'

You can use “tuple unpacking”:

>>> my_list = [(1, 'abc'), (2, 'def')]
>>> my_ids = [idx for idx, val in my_list]
>>> my_ids
[1, 2]

At iteration time each tuple is unpacked and its values are set to the variables idx and val.

>>> x = (1, 'abc')
>>> idx, val = x
>>> idx
1
>>> val
'abc'

回答 4

operator.itemgetter是为了什么。

>>> a = [(1, u'abc'), (2, u'def')]
>>> import operator
>>> b = map(operator.itemgetter(0), a)
>>> b
[1, 2]

itemgetter语句返回一个函数该函数返回您指定的元素的索引。和写完全一样

>>> b = map(lambda x: x[0], a)

但是我发现这itemgetter是一个更清晰,更明确的说法。

这对于制作紧凑的排序语句非常方便。例如,

>>> c = sorted(a, key=operator.itemgetter(0), reverse=True)
>>> c
[(2, u'def'), (1, u'abc')]

This is what operator.itemgetter is for.

>>> a = [(1, u'abc'), (2, u'def')]
>>> import operator
>>> b = map(operator.itemgetter(0), a)
>>> b
[1, 2]

The itemgetter statement returns a function that returns the index of the element you specify. It’s exactly the same as writing

>>> b = map(lambda x: x[0], a)

But I find that itemgetter is a clearer and more explicit.

This is handy for making compact sort statements. For example,

>>> c = sorted(a, key=operator.itemgetter(0), reverse=True)
>>> c
[(2, u'def'), (1, u'abc')]

回答 5

从性能的角度来看,在python3.X中

  • [i[0] for i in a]list(zip(*a))[0]等价
  • 他们比 list(map(operator.itemgetter(0), a))

import timeit


iterations = 100000
init_time = timeit.timeit('''a = [(i, u'abc') for i in range(1000)]''', number=iterations)/iterations
print(timeit.timeit('''a = [(i, u'abc') for i in range(1000)]\nb = [i[0] for i in a]''', number=iterations)/iterations - init_time)
print(timeit.timeit('''a = [(i, u'abc') for i in range(1000)]\nb = list(zip(*a))[0]''', number=iterations)/iterations - init_time)

输出

3.491014136001468e-05

3.422205176000717e-05

From a performance point of view, in python3.X

  • [i[0] for i in a] and list(zip(*a))[0] are equivalent
  • they are faster than list(map(operator.itemgetter(0), a))

Code

import timeit


iterations = 100000
init_time = timeit.timeit('''a = [(i, u'abc') for i in range(1000)]''', number=iterations)/iterations
print(timeit.timeit('''a = [(i, u'abc') for i in range(1000)]\nb = [i[0] for i in a]''', number=iterations)/iterations - init_time)
print(timeit.timeit('''a = [(i, u'abc') for i in range(1000)]\nb = list(zip(*a))[0]''', number=iterations)/iterations - init_time)

output

3.491014136001468e-05

3.422205176000717e-05


回答 6

如果元组是唯一的,那么这可以工作

>>> a = [(1, u'abc'), (2, u'def')]
>>> a
[(1, u'abc'), (2, u'def')]
>>> dict(a).keys()
[1, 2]
>>> dict(a).values()
[u'abc', u'def']
>>> 

if the tuples are unique then this can work

>>> a = [(1, u'abc'), (2, u'def')]
>>> a
[(1, u'abc'), (2, u'def')]
>>> dict(a).keys()
[1, 2]
>>> dict(a).values()
[u'abc', u'def']
>>> 

回答 7

当我跑步时(如上所述):

>>> a = [(1, u'abc'), (2, u'def')]
>>> import operator
>>> b = map(operator.itemgetter(0), a)
>>> b

而不是返回:

[1, 2]

我收到此作为回报:

<map at 0xb387eb8>

我发现我必须使用list():

>>> b = list(map(operator.itemgetter(0), a))

使用此建议成功返回列表。也就是说,我对这个解决方案感到满意,谢谢。(使用Spyder,iPython控制台,Python v3.6测试/运行)

when I ran (as suggested above):

>>> a = [(1, u'abc'), (2, u'def')]
>>> import operator
>>> b = map(operator.itemgetter(0), a)
>>> b

instead of returning:

[1, 2]

I received this as the return:

<map at 0xb387eb8>

I found I had to use list():

>>> b = list(map(operator.itemgetter(0), a))

to successfully return a list using this suggestion. That said, I’m happy with this solution, thanks. (tested/run using Spyder, iPython console, Python v3.6)


回答 8

我当时认为比较不同方法的运行时可能很有用,所以我做了一个基准测试(使用simple_benchmark库)

I)具有2个元素的元组的基准

您可能希望通过索引从元组中选择第一个元素0,这是最快的解决方案,非常接近拆包解决方案,因为恰好需要两个值

import operator
import random

from simple_benchmark import BenchmarkBuilder

b = BenchmarkBuilder()



@b.add_function()
def rakesh_by_index(l):
    return [i[0] for i in l]


@b.add_function()
def wayneSan_zip(l):
    return list(list(zip(*l))[0])


@b.add_function()
def bcattle_itemgetter(l):
     return list(map(operator.itemgetter(0), l))


@b.add_function()
def ssoler_upacking(l):
    return [idx for idx, val in l]

@b.add_function()
def kederrack_unpacking(l):
    return [f for f, *_ in l]



@b.add_arguments('Number of tuples')
def argument_provider():
    for exp in range(2, 21):
        size = 2**exp
        yield size, [(random.choice(range(100)), random.choice(range(100))) for _ in range(size)]


r = b.run()
r.plot()

II)具有两个或多个元素的元组的基准

import operator
import random

from simple_benchmark import BenchmarkBuilder

b = BenchmarkBuilder()

@b.add_function()
def kederrack_unpacking(l):
    return [f for f, *_ in l]


@b.add_function()
def rakesh_by_index(l):
    return [i[0] for i in l]


@b.add_function()
def wayneSan_zip(l):
    return list(list(zip(*l))[0])


@b.add_function()
def bcattle_itemgetter(l):
     return list(map(operator.itemgetter(0), l))


@b.add_arguments('Number of tuples')
def argument_provider():
    for exp in range(2, 21):
        size = 2**exp
        yield size, [tuple(random.choice(range(100)) for _
                     in range(random.choice(range(2, 100)))) for _ in range(size)]

from pylab import rcParams
rcParams['figure.figsize'] = 12, 7

r = b.run()
r.plot()

I was thinking that it might be useful to compare the runtimes of the different approaches so I made a benchmark (using simple_benchmark library)

I) Benchmark having tuples with 2 elements

As you may expect to select the first element from tuples by index 0 shows to be the fastest solution very close to the unpacking solution by expecting exactly 2 values

import operator
import random

from simple_benchmark import BenchmarkBuilder

b = BenchmarkBuilder()



@b.add_function()
def rakesh_by_index(l):
    return [i[0] for i in l]


@b.add_function()
def wayneSan_zip(l):
    return list(list(zip(*l))[0])


@b.add_function()
def bcattle_itemgetter(l):
     return list(map(operator.itemgetter(0), l))


@b.add_function()
def ssoler_upacking(l):
    return [idx for idx, val in l]

@b.add_function()
def kederrack_unpacking(l):
    return [f for f, *_ in l]



@b.add_arguments('Number of tuples')
def argument_provider():
    for exp in range(2, 21):
        size = 2**exp
        yield size, [(random.choice(range(100)), random.choice(range(100))) for _ in range(size)]


r = b.run()
r.plot()

II) Benchmark having tuples with 2 or more elements

import operator
import random

from simple_benchmark import BenchmarkBuilder

b = BenchmarkBuilder()

@b.add_function()
def kederrack_unpacking(l):
    return [f for f, *_ in l]


@b.add_function()
def rakesh_by_index(l):
    return [i[0] for i in l]


@b.add_function()
def wayneSan_zip(l):
    return list(list(zip(*l))[0])


@b.add_function()
def bcattle_itemgetter(l):
     return list(map(operator.itemgetter(0), l))


@b.add_arguments('Number of tuples')
def argument_provider():
    for exp in range(2, 21):
        size = 2**exp
        yield size, [tuple(random.choice(range(100)) for _
                     in range(random.choice(range(2, 100)))) for _ in range(size)]

from pylab import rcParams
rcParams['figure.figsize'] = 12, 7

r = b.run()
r.plot()

回答 9

我想知道为什么没人建议使用numpy,但是现在检查后我明白了。对于混合类型数组,可能不是最好的方法。

这将是numpy中的解决方案:

>>> import numpy as np

>>> a = np.asarray([(1, u'abc'), (2, u'def')])
>>> a[:, 0].astype(int).tolist()
[1, 2]

I wondered why nobody suggested to use numpy, but now after checking i understand. It is maybe not the best for mixed type arrays.

This would be a solution in numpy:

>>> import numpy as np

>>> a = np.asarray([(1, u'abc'), (2, u'def')])
>>> a[:, 0].astype(int).tolist()
[1, 2]

回答 10

这些是元组,而不是集合。你可以这样做:

l1 = [(1, u'abc'), (2, u'def')]
l2 = [(tup[0],) for tup in l1]
l2
>>> [(1,), (2,)]

Those are tuples, not sets. You can do this:

l1 = [(1, u'abc'), (2, u'def')]
l2 = [(tup[0],) for tup in l1]
l2
>>> [(1,), (2,)]

回答 11

您可以使用列表推导解压缩元组并仅获取第一个元素:

l = [(1, u'abc'), (2, u'def')]
[f for f, *_ in l]

输出:

[1, 2]

无论您在一个元组中有多少个元素,这都将起作用:

l = [(1, u'abc'), (2, u'def', 2, 4, 5, 6, 7)]
[f for f, *_ in l]

输出:

[1, 2]

you can unpack your tuples and get only the first element using a list comprehension:

l = [(1, u'abc'), (2, u'def')]
[f for f, *_ in l]

output:

[1, 2]

this will work no matter how many elements you have in a tuple:

l = [(1, u'abc'), (2, u'def', 2, 4, 5, 6, 7)]
[f for f, *_ in l]

output:

[1, 2]