Python元组列表到int列表 [英] Python list of tuples to list of int

查看:221
本文介绍了Python元组列表到int列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,我有x=[(12,), (1,), (3,)](元组列表),我想以最佳方式获取x=[12, 1, 3](整数列表)吗?你能帮忙吗?

So, I have x=[(12,), (1,), (3,)] (list of tuples) and I want x=[12, 1, 3] (list of integers) in best way possible? Can you please help?

推荐答案

您没有说最好"的意思,但是大概是最pythonic"或最易读"或类似的意思.

You didn't say what you mean by "best", but presumably you mean "most pythonic" or "most readable" or something like that.

F3AR3DLEGEND给出的列表理解可能是最简单的.任何知道如何阅读列表理解的人都会立即知道它的含义.

The list comprehension given by F3AR3DLEGEND is probably the simplest. Anyone who knows how to read a list comprehension will immediately know what it means.

y = [i[0] for i in x]

但是,通常您实际上并不需要列表,而只是可以迭代一次的列表.如果您在x中有十亿个元素,那么只构建一个十亿个元素y以便一次迭代一个元素可能是一个坏主意.因此,您可以使用生成器表达式:

However, often you don't really need a list, just something that can be iterated over once. If you've got a billion elements in x, building a billion-element y just to iterate over it one element at a time may be a bad idea. So, you can use a generator expression:

y = (i[0] for i in x)

如果您喜欢函数式编程,则可能更喜欢使用map. map的缺点是您必须向其传递一个函数,而不仅仅是一个表达式,这意味着您要么需要使用lambda函数,要么要使用itemgetter:

If you prefer functional programming, you might prefer to use map. The downside of map is that you have to pass it a function, not just an expression, which means you either need to use a lambda function, or itemgetter:

y = map(operator.itemgetter(0), x)

在Python 3中,这等效于生成器表达式;如果需要list,请将其传递给list.在Python 2中,它返回list;如果需要迭代器,请使用itertools.imap而不是map.

In Python 3, this is equivalent to the generator expression; if you want a list, pass it to list. In Python 2, it returns a list; if you want an iterator, use itertools.imap instead of map.

如果您想要一个更通用的拼合解决方案,可以自己编写一个,但始终值得参考 itertools 用于此类通用解决方案,实际上有一个名为flatten的配方用于平整嵌套的一层".因此,将其复制并粘贴到您的代码(或pip install more-itertools)中,您可以执行以下操作:

If you want a more generic flattening solution, you can write one yourself, but it's always worth looking at itertools for generic solutions of this kind, and there is in fact a recipe called flatten that's used to "Flatten one level of nesting". So, copy and paste that into your code (or pip install more-itertools) and you can just do this:

y = flatten(x)

如果您查看如何实现flatten,然后研究如何实现chain.from_iterable,然后研究如何实现chain,您会注意到,就内置而言,您可以编写相同的东西.但是,当flatten更具可读性和明显性时,为什么还要打扰呢?

If you look at how flatten is implemented, and then at how chain.from_iterable is implemented, and then at how chain is implemented, you'll notice that you could write the same thing in terms of builtins. But why bother, when flatten is going to be more readable and obvious?

最后,如果您想将通用版本简化为嵌套列表理解(当然是生成器表达式):

Finally, if you want to reduce the generic version to a nested list comprehension (or generator expression, of course):

y = [j for i in x for j in i]

但是,无论在书写还是阅读中,嵌套列表推导都很容易出错. (请注意,首先给出最简单答案的那个人F3AR3DLEGEND也给出了嵌套的理解,并弄错了.如果他不能解决问题,您确定要尝试吗?)对于非常简单的情况,他们是不太糟糕,但我仍然认为flatten更容易阅读.

However, nested list comprehensions are very easy to get wrong, both in writing and reading. (Note that F3AR3DLEGEND, the same person who gave the simplest answer first, also gave a nested comprehension and got it wrong. If he can't pull it off, are you sure you want to try?) For really simple cases, they're not too bad, but still, I think flatten is a lot easier to read.

这篇关于Python元组列表到int列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆