Python:无论如何使用map来获取元组的第一个元素

发布于 2024-11-07 00:16:22 字数 291 浏览 5 评论 0原文

我有一个元组的元组,我想将每个元组中的第一个值放入一个集合中。我认为使用 map() 是一个很好的方法,唯一的事情是我找不到一种简单的方法来访问元组中的第一个元素。例如,我有元组 ((1,), (3,))。我想做类似 set(map([0], ((1,), (3,)))) 的事情(其中 [0] 正在访问第零个元素)得到一个包含 1 和 3 的集合。我能想到的唯一方法是定义一个函数:def first(t): return t[0]。无论如何,是否可以在一行中完成此操作而无需声明该函数?

I have a tuple of tuples and I want to put the first value in each of the tuples into a set. I thought using map() would be a good way of doing this the only thing is I can't find an easy way to access the first element in the tuple. So for example I have the tuple ((1,), (3,)). I'd like to do something like set(map([0], ((1,), (3,)))) (where [0] is accessing the zeroth element) to get a set with 1 and 3 in it. The only way I can figure to do it is to define a function: def first(t): return t[0]. Is there anyway of doing this in one line without having to declare the function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

久夏青 2024-11-14 00:16:22

使用列表理解:

data = ((1,), (3,))
print([x[0] for x in data])

Use a list comprehension:

data = ((1,), (3,))
print([x[0] for x in data])
暗喜 2024-11-14 00:16:22

使用 operator.itemgetter

from operator import itemgetter
map(itemgetter(0), ((1,), (3,)))

:列表理解通常更具可读性, itemgetter 最接近您的要求。它也更快一点:

>>> from timeit import timeit
>>> setup = 'from operator import itemgetter; lst=( ("a",), ("b",), (1,), (2,))'
>>> timeit('map(itemgetter(0), lst)', setup=setup)
0.13061050399846863
>>> timeit('[i[0] for i in lst]', setup=setup)
0.20302422800159547

Use operator.itemgetter:

from operator import itemgetter
map(itemgetter(0), ((1,), (3,)))

While the list comprehensions are generally more readable, itemgetter is closest to what you asked for. It's also a bit faster:

>>> from timeit import timeit
>>> setup = 'from operator import itemgetter; lst=( ("a",), ("b",), (1,), (2,))'
>>> timeit('map(itemgetter(0), lst)', setup=setup)
0.13061050399846863
>>> timeit('[i[0] for i in lst]', setup=setup)
0.20302422800159547
不美如何 2024-11-14 00:16:22
my_set = {x[0] for x in TUPLES}
my_set = {x[0] for x in TUPLES}
巴黎夜雨 2024-11-14 00:16:22

还有另一种获取方式:

set(x for x, in data)

Just another way to get it:

set(x for x, in data)
厌倦 2024-11-14 00:16:22

Python 支持使用 lambda 创建匿名函数a> 关键字。这允许您使用函数而无需正式定义它。给定您的示例,您将像这样使用 lambda:

data = ((1,), (3,))
set(map(lambda x: x[0], data))

这相当于:

def f(x):
    return x[0]

set(map(f, data))

但正如其他人所说,列表推导式优于使用映射。

Python supports the creation of anonymous function using the lambda keyword. This allows you to use a function without formally defining it. Given your example, you'd use the lambda like this:

data = ((1,), (3,))
set(map(lambda x: x[0], data))

This is equivalent to:

def f(x):
    return x[0]

set(map(f, data))

But as other people have said, list comprehensions are preferred over the use of map.

油焖大侠 2024-11-14 00:16:22

您可以在 Python 2.7 和 3.x 中使用集合理解:

>>> t = ((1,), (3,))
>>> s = {x[0] for x in t}
>>> s
set([1, 3])

或在 Python < 2.7:

>>> s = set([x[0] for x in t])
>>> s
set([1, 3])

You can use a set comprehension in Python 2.7 and 3.x:

>>> t = ((1,), (3,))
>>> s = {x[0] for x in t}
>>> s
set([1, 3])

or in Python < 2.7:

>>> s = set([x[0] for x in t])
>>> s
set([1, 3])
贩梦商人 2024-11-14 00:16:22
data = ((1,), (3,))
s = set(zip(*data)[0])

如果元组中有更多项目,您可以使用 izip 和 islice 节省一些内存和时间。

data = ((1,), (3,))
s = set(zip(*data)[0])

If there are more items in your tuples you might save some memory and time using izip and islice.

假装不在乎 2024-11-14 00:16:22

和@Winston 一起去。列表理解非常棒。如果您确实想要使用映射,请按照之前的建议使用 lambda,或者逻辑上等效的...

from operator import itemgetter
data = ((1,), (3,))
map(itemgetter(0), data)

这仅供参考;您应该使用列表比较

Go with @Winston. List comprehensions are great. If you really want to use map, use a lambda as previously suggested, or the logically equivalent...

from operator import itemgetter
data = ((1,), (3,))
map(itemgetter(0), data)

This is just for info; You should use the list comp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文