如何在列表中反转元组? [英] How to reverse tuples in a list?

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

问题描述

我有一个元组列表

[('''',1.0),(''b'',2.0),(''c'',3.0)]

我想颠倒元组中元素的顺序。

[(1.0,''a''),(2.0,''b''),(3.0 ,''c'')]


我知道我可以长篇形式:

q = []

y = [('''',1.0),(''b'',2.0),(''c'',3.0)]

for y in y:

t = list(t)

t.reverse()

q.append(tuple(t))

y = q


但似乎应该有一个聪明的方法来实现这一点

a list comprehensions。问题是我无法看到如何对列表中的每个元组应用

reverse()因为反向()a

列表方法(不是元组方法)和因为它在原地运作

(不返回值)。这种残骸在列表理解中做了b / b
。我想说的是

这个:

y = [t.reverse()for t in y]

Even如果在元组上反向工作,它就不会在

列表理解中工作。


你的,

Noah

I have a list of tuples
[(''a'', 1.0), (''b'', 2.0), (''c'', 3.0)]
I want to reverse the order of the elements inside the tuples.
[(1.0,''a''), (2.0, ''b''), (3.0, ''c'')]

I know I could do this long-form:
q = []
y = [(''a'', 1.0), (''b'', 2.0), (''c'', 3.0)]
for i in y:
t=list(t)
t.reverse()
q.append(tuple(t))
y = q

But it seems like there should be a clever way to do this with
a list comprehensions. Problem is I can''t see how to apply
reverse() to each tuple in the list because reverse() a
list method (not a tuple method) and because it operates
in-place (does not return a value). This kind of wrecks doing
it in a list comprehension. What I''d like to say is something like
this:
y = [t.reverse() for t in y]
Even if reverse worked on tuples, it wouldn''t work inside a
list comprehension.

Yours,
Noah

推荐答案

Noah写道:
Noah wrote:

但似乎应该有一个聪明的方式用

a列表理解。问题是我无法看到如何对列表中的每个元组应用

reverse()因为反向()a

列表方法(不是元组方法)和因为它在原地运作

(不返回值)。这种残骸在列表理解中做了b / b
。我想说的是

这个:

y = [t.reverse()for t in y]

Even如果在元组上反向工作,它将不会在

列表理解中工作。
But it seems like there should be a clever way to do this with
a list comprehensions. Problem is I can''t see how to apply
reverse() to each tuple in the list because reverse() a
list method (not a tuple method) and because it operates
in-place (does not return a value). This kind of wrecks doing
it in a list comprehension. What I''d like to say is something like
this:
y = [t.reverse() for t in y]
Even if reverse worked on tuples, it wouldn''t work inside a
list comprehension.



为什么要用列表推导来做?使用逆转:

Why would you want to do it with list comprehensions? Use reversed:


>> t =(1,2,3)
u =元组(反向(t))
u
>>t = (1, 2, 3)
u = tuple(reversed(t))
u



(3,2,1)
-

Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& AIM erikmaxfrancis

结束战争的最快方法就是失去它。

- George Orwell,1903-1950

(3, 2, 1)
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
The quickest way of ending a war is to lose it.
-- George Orwell, 1903-1950


Noah写道:
Noah wrote:

我有一个元组列表

[('''',1.0),('' b'',2.0),(''c'',3.0)]

我想颠倒元组内部元素的顺序。

[(1.0, ''a''),(2.0,''b''),(3.0,''c'')]


我知道我可以做长形式:

q = []

y = [('''',1.0),(''b'',2.0),(''c'',3.0)]

for y in y:

t = list(t)

t.reverse()

q.append (元组(t))

y = q


但似乎应该有一个聪明的方法来做到这一点

a列表理解。问题是我无法看到如何对列表中的每个元组应用

reverse()因为反向()a

列表方法(不是元组方法)和因为它在原地运作

(不返回值)。这种残骸在列表理解中做了b / b
。我想说的是

这个:

y = [t.reverse()for t in y]

Even如果在元组上反向工作,它就不会在

列表理解中工作。


你的,

Noah
I have a list of tuples
[(''a'', 1.0), (''b'', 2.0), (''c'', 3.0)]
I want to reverse the order of the elements inside the tuples.
[(1.0,''a''), (2.0, ''b''), (3.0, ''c'')]

I know I could do this long-form:
q = []
y = [(''a'', 1.0), (''b'', 2.0), (''c'', 3.0)]
for i in y:
t=list(t)
t.reverse()
q.append(tuple(t))
y = q

But it seems like there should be a clever way to do this with
a list comprehensions. Problem is I can''t see how to apply
reverse() to each tuple in the list because reverse() a
list method (not a tuple method) and because it operates
in-place (does not return a value). This kind of wrecks doing
it in a list comprehension. What I''d like to say is something like
this:
y = [t.reverse() for t in y]
Even if reverse worked on tuples, it wouldn''t work inside a
list comprehension.

Yours,
Noah



如果数据保持不变[(a,b),...]

Python 2.5a2(r25a2:45740,2006年5月24日) ,19:50:20)

[GCC 3.3.6]关于linux2

Provided the data remains the same [(a, b), ...]
Python 2.5a2 (r25a2:45740, May 24 2006, 19:50:20)
[GCC 3.3.6] on linux2


>> x = [('''',1.0),(''b'',2.0),(''c'',3.0)]
y = [( b,a)a,b in x]
>>x = [(''a'', 1.0), (''b'', 2.0), (''c'', 3.0)]
y = [(b, a) for a, b in x]
y



[(1.0,''a'') ,(2.0,''b''),(3.0,''c'')]

希望这会有所帮助,

Adonis

[(1.0, ''a''), (2.0, ''b''), (3.0, ''c'')]
Hope this helps,
Adonis


Noah写道:
Noah wrote:

我有一个元组列表

[(''a'',1.0) ,(''b' ,2.0),(''c'',3.0)]

我想颠倒元组内部元素的顺序。

[(1.0,''a ''),(2.0,''b''),(3.0,''c'')]


我知道我可以做长形式:

q = []

y = [('''',1.0),(''b'',2.0),(''c'',3.0)]

for i in y:

t = list(t)

t.reverse()

q.append(tuple( t))

y = q


但似乎应该有一个聪明的方法用

a列表理解来做到这一点。问题是我无法看到如何对列表中的每个元组应用

reverse()因为反向()a

列表方法(不是元组方法)和因为它在原地运作

(不返回值)。这种残骸在列表理解中做了b / b
。我想说的是

这个:

y = [t.reverse()for t in y]

Even如果在元组上反向工作,它就不会在

列表理解中工作。


你的,

Noah
I have a list of tuples
[(''a'', 1.0), (''b'', 2.0), (''c'', 3.0)]
I want to reverse the order of the elements inside the tuples.
[(1.0,''a''), (2.0, ''b''), (3.0, ''c'')]

I know I could do this long-form:
q = []
y = [(''a'', 1.0), (''b'', 2.0), (''c'', 3.0)]
for i in y:
t=list(t)
t.reverse()
q.append(tuple(t))
y = q

But it seems like there should be a clever way to do this with
a list comprehensions. Problem is I can''t see how to apply
reverse() to each tuple in the list because reverse() a
list method (not a tuple method) and because it operates
in-place (does not return a value). This kind of wrecks doing
it in a list comprehension. What I''d like to say is something like
this:
y = [t.reverse() for t in y]
Even if reverse worked on tuples, it wouldn''t work inside a
list comprehension.

Yours,
Noah



如果你的元组都是两个项目,你可以这样做:


y = [(b,a)for a ,b in y]


如果没有,那么:


y = [tuple(reversed(t))for t in y]


:-D


和平,

~西蒙

If your tuples are all two items, you can do it like this:

y = [(b, a) for a, b in y]

if not, then:

y = [tuple(reversed(t)) for t in y]

:-D

Peace,
~Simon


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

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