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 11 16722
Noah wrote:
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 = tuple(reversed(t)) u
(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 wrote:
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
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 = [('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')]
Hope this helps,
Adonis
Noah wrote:
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
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
Noah wrote:
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')]
Python 2.4+:
y = [tuple(reversed(t)) for t in y]
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Erik Max Francis wrote:
Noah wrote:
>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?
Because he has a list of tuples and wants to reverse each individual tuple in
the list.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
"Noah" <no**@noah.orgwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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')]
>>tups = [('a', 1.0), ('b', 2.0), ('c', 3.0)] map(tuple,map(reversed,tups))
[(1.0, 'a'), (2.0, 'b'), (3.0, 'c')]
"Noah" <no**@noah.orgwrites:
I know I could do this long-form:
q = []
y = [('a', 1.0), ('b', 2.0), ('c', 3.0)]
...
y = [tuple(reversed(t)) for t in y]
Noah wrote:
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')]
Python 2.4.x :
y = [tuple(reversed(t)) for t in y]
Python < 2.4.x:
def reverse_tuple(t):
t = list(t)
t.reverse()
return tuple(t)
y = [reverse_tuple(t) for t in y]
HTH
Applying the perl motto (there is more than one way to do it) sure enough leads
to a perlish solution... as measured by line noise.
>>t = [('a', 11,1.0), ('b',22,2.0),('c',33,3.0)] zip(*zip(*t)[::-1])
[(1.0, 11, 'a'), (2.0, 22, 'b'), (3.0, 33, 'c')]
Robert Kern <ro*********@gmail.comwrote:
>Python 2.4+:
y = [tuple(reversed(t)) for t in y]
Python 2.3:
y = [ t[::-1] for t in y ]
Obviously works in 2.4 as well, where I make it faster than using
tuple(reversed(t)). Which isn't surprising, as it's not constructing
the intermediate list.
--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Noah wrote (among other things ;)):
I have a list of tuples
I want to reverse the order of the elements inside the tuples.
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.
This is a minor point, but I've got the impression you're not completely
aware that tuples are immutable, and what that implies. I mean that
t.reverse(), assuming the same semantics as the list method, is just not
possible, as it would try to alter the tuple. This is not what tuples
are for, so there.
Sorry if you knew this, just wanted to point out that there is a
conscious difference between tuples and lists.
This is of course just a half-newbie crackin wise, so forgive me ;).
wildemar This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ivan Voras |
last post by:
Are there any performance/size differences between using tuples and using
lists?
--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke
|
by: Raymond Hettinger |
last post by:
Here is a discussion draft of a potential PEP.
The ideas grew out of the discussion on pep-284.
Comments are invited. Dart throwing is optional.
Raymond Hettinger
...
|
by: Raymond Hettinger |
last post by:
Please comment on the new PEP for reverse iteration methods.
Basically, the idea looks like this:
for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0
<do something with i>
The...
|
by: Raymond Hettinger |
last post by:
Based on your extensive feedback, PEP 322 has been completely revised.
The response was strongly positive, but almost everyone preferred
having a function instead of multiple object methods. The...
|
by: Jeff Wagner |
last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what
you can do with them. I am still left with a question and that is, when should you choose a list or...
|
by: Thorsten Kampe |
last post by:
I found out that I am rarely using tuples and almost always lists
because of the more flexible usability of lists (methods, etc.)
To my knowledge, the only fundamental difference between tuples...
|
by: tkpmep |
last post by:
I have list of lists of the following form
L=, , , ]
I want to aggregate these lists, i.e. to reduce L to
L=, ] #500 = 100+400, 200=300-100
Here's how I have done it:
L.sort()
for i in...
|
by: rshepard |
last post by:
While working with lists of tuples is probably very common, none of my
five Python books or a Google search tell me how to refer to specific items
in each tuple. I find references to sorting a list...
|
by: Chris Rebert |
last post by:
On Thu, Oct 2, 2008 at 8:07 PM, David Di Biase <dave.dibiase@gmail.comwrote:
Rather than defining a comparison function here (which is less
efficient), you can use the 'key' argument, which...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |