Python元组列表到int列表

时间:2022-06-20 20:27:32

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?

那么,我有x=[(12,)(1,)(3,)](元组列表)我想要x=[12, 1, 3](整数列表)以最好的方式?你能请帮助吗?

5 个解决方案

#1


11  

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

你没有说“最佳”是什么意思,但你大概是指“最python化的”或“最易读的”之类的。

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.

F3AR3DLEGEND提供的列表理解可能是最简单的。任何知道如何阅读列表理解的人都会立刻知道它的意思。

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

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:

然而,通常您并不需要列表,只需要可以重复一次的东西。如果x中有10亿个元素,构建一个10亿个元素的y来迭代它一次一个元素可能不是一个好主意。因此,您可以使用生成器表达式:

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

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:

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

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

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.

在Python 3中,这相当于生成器表达式;如果您想要一个列表,请将它传递给列表。在Python 2中,它返回一个列表;如果需要迭代器,请使用迭代工具。imap的地图。

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:

如果您想要一个更通用的扁平化解决方案,您可以自己编写一个,但是对于这类通用解决方案来说,总是值得查看迭代工具,实际上有一个名为flatten的配方,用来“平坦一层的嵌套”。因此,复制并粘贴到您的代码中(或者pip安装more-itertools),您可以这样做:

y = flatten(x)

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?

如果您了解flatten的实现方式,以及chain.from_iterable的实现方式,以及chain的实现方式,您会注意到您可以用build - in来写同样的东西。但是为什么要这么麻烦呢?

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]

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.

然而,嵌套列表理解很容易出错,无论是在写作还是阅读中。(请注意,F3AR3DLEGEND也是最先给出最简单答案的那个人,也给出了嵌套的理解,并出错了。如果他做不到,你确定你想试试吗?对于非常简单的情况,它们不是很糟糕,但我仍然认为flatten更容易读。

#2


5  

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

This only works for one element per tuple, though.

不过,这只适用于每个元组中的一个元素。

However, if you have multiple elements per tuple, you can use a slightly more complex list comprehension:

但是,如果每个元组中有多个元素,则可以使用稍微复杂一点的列表理解:

y = [i[j] for i in x for j in range(len(i))]

Reference: List Comprehensions

参考:列表理解

#3


2  

Just do this:

只是这样做:

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

Explanation:

解释:

>>> x=[(12,), (1,), (3,)]

>>> x
[(12,), (1,), (3,)]

>>> [i for i in x]
[(12,), (1,), (3,)]

>>> [i[0] for i in x]
[12, 1, 3]

#4


1  

This is the most efficient way:

这是最有效的方法:

x = [i for i, in x]

or, equivalently

或者,同样

x = [i for (i,) in x]

This is a bit slower:

这有点慢:

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

#5


0  

you can use map function....

您可以使用map函数....

map(lambda y: y[0], x)

#1


11  

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

你没有说“最佳”是什么意思,但你大概是指“最python化的”或“最易读的”之类的。

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.

F3AR3DLEGEND提供的列表理解可能是最简单的。任何知道如何阅读列表理解的人都会立刻知道它的意思。

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

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:

然而,通常您并不需要列表,只需要可以重复一次的东西。如果x中有10亿个元素,构建一个10亿个元素的y来迭代它一次一个元素可能不是一个好主意。因此,您可以使用生成器表达式:

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

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:

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

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

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.

在Python 3中,这相当于生成器表达式;如果您想要一个列表,请将它传递给列表。在Python 2中,它返回一个列表;如果需要迭代器,请使用迭代工具。imap的地图。

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:

如果您想要一个更通用的扁平化解决方案,您可以自己编写一个,但是对于这类通用解决方案来说,总是值得查看迭代工具,实际上有一个名为flatten的配方,用来“平坦一层的嵌套”。因此,复制并粘贴到您的代码中(或者pip安装more-itertools),您可以这样做:

y = flatten(x)

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?

如果您了解flatten的实现方式,以及chain.from_iterable的实现方式,以及chain的实现方式,您会注意到您可以用build - in来写同样的东西。但是为什么要这么麻烦呢?

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]

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.

然而,嵌套列表理解很容易出错,无论是在写作还是阅读中。(请注意,F3AR3DLEGEND也是最先给出最简单答案的那个人,也给出了嵌套的理解,并出错了。如果他做不到,你确定你想试试吗?对于非常简单的情况,它们不是很糟糕,但我仍然认为flatten更容易读。

#2


5  

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

This only works for one element per tuple, though.

不过,这只适用于每个元组中的一个元素。

However, if you have multiple elements per tuple, you can use a slightly more complex list comprehension:

但是,如果每个元组中有多个元素,则可以使用稍微复杂一点的列表理解:

y = [i[j] for i in x for j in range(len(i))]

Reference: List Comprehensions

参考:列表理解

#3


2  

Just do this:

只是这样做:

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

Explanation:

解释:

>>> x=[(12,), (1,), (3,)]

>>> x
[(12,), (1,), (3,)]

>>> [i for i in x]
[(12,), (1,), (3,)]

>>> [i[0] for i in x]
[12, 1, 3]

#4


1  

This is the most efficient way:

这是最有效的方法:

x = [i for i, in x]

or, equivalently

或者,同样

x = [i for (i,) in x]

This is a bit slower:

这有点慢:

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

#5


0  

you can use map function....

您可以使用map函数....

map(lambda y: y[0], x)