The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Reordering value?
Discuss Reordering value? in the Python Programming forum on Dev Shed. Reordering value? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 27th, 2013, 05:01 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 27
Time spent in forums: 17 h 19 m 39 sec
Reputation Power: 0
|
|
|
Reordering value?
If I just want to reorder the value inside my list, how would I do that? Would I use the sorted() function or would I do something like orderedDictionary?
So for example, my output is this right now:
Code:
[('1', 'China', '1,904'), ('2', 'United States', '1,497'), ('3', 'Germany', '1,408'), ('4', 'Japan', '788'), ('5', 'France', '587.1')....('221', 'Tokelau', '0')]
The order is right in that the list is ranked 1-221 so I don't want to change that.
I initially just want to switch the values inside the parentheses so that my output is something like this:
Code:
[('China', '1,904', '1'), ('United States', '1,497', '2'), ('Germany', '1,408', '3'), ('Japan', '788', '4'), ('France', '587.1', '5')....('Tokelau', '0', '221')]
When I used the sorted function, it sorted the whole things so I don't think that's what I need...
Sry, this is probably something super easy that I'm just not getting...
|

January 27th, 2013, 05:32 PM
|
 |
Contributing User
|
|
|
|
The ordered dictionary preserves the sequence in which items are added. Won't help.
('tuples', 'are', 'immutable')
You can make new tuples.
Code:
>>> [(b,c,a,) for (a,b,c,) in [('1', 'China', '1,904'), ('2', 'United States', '1,497')]]
[('China', '1,904', '1'), ('United States', '1,497', '2')]
__________________
[code] Code tags[/code] are essential for python code!
|

January 27th, 2013, 06:15 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 27
Time spent in forums: 17 h 19 m 39 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by b49P23TIvg The ordered dictionary preserves the sequence in which items are added. Won't help.
('tuples', 'are', 'immutable')
You can make new tuples.
Code:
>>> [(b,c,a,) for (a,b,c,) in [('1', 'China', '1,904'), ('2', 'United States', '1,497')]]
[('China', '1,904', '1'), ('United States', '1,497', '2')]
|
So I think I get what you're saying and tried this:
Code:
tuples = re.findall(r"<td align='right'>([^>]+)</td><td><a href='../\w+/exports.html'>([^>]+)</a></td><td align='right'>([^>]+)</td>", text)
#print tuples
country_to_rank = {}
country_to_rank = [tuple(reversed(tuples)) for tuples in tuple]
print country_to_rank
But it gave me an error.
Edit:
I also just tried it this way, but instead of an error, it gives me empty []...
Code:
tuples = re.findall(r"<td align='right'>([^>]+)</td><td><a href='../\w+/exports.html'>([^>]+)</a></td><td align='right'>([^>]+)</td>", text)
#print tuples
country_to_rank = {}
country_to_rank = [tuple(reversed(tuples)) for tuples in country_to_rank]
print country_to_rank
|

January 27th, 2013, 07:43 PM
|
 |
Contributing User
|
|
|
|
I worried about giving a list comprehension. Yes---it was a bad idea.
Code:
>>> print(tuples)
[('1', 'China', '1,904'), ('2', 'United States', '1,497'), ('3', 'Germany', '1,408'), ('4', 'Japan', '788'), ('5', 'France', '587.1')]
>>> country_to_rank = [tuple(reversed(t)) for t in tuples]
>>> print(country_to_rank)
[('1,904', 'China', '1'), ('1,497', 'United States', '2'), ('1,408', 'Germany', '3'), ('788', 'Japan', '4'), ('587.1', 'France', '5')]
>>>
Code:
>>> country_to_rank = []
>>> for t in tuples:
... r = tuple(reversed(t))
... country_to_rank.append(r)
...
>>> print(country_to_rank)
[('1,904', 'China', '1'), ('1,497', 'United States', '2'), ('1,408', 'Germany', '3'), ('788', 'Japan', '4'), ('587.1', 'France', '5')]
>>>
|

January 27th, 2013, 08:08 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 27
Time spent in forums: 17 h 19 m 39 sec
Reputation Power: 0
|
|
|
Hmm...okay, is there a way to add like a value or something like an if statement after the for t in tuples line so that the country displays first?
[('1,904', 'China', '1')] --> [('China', '1,904', '1')]
What about using the reverse again? But I think that would just be going in a loop...
Or am I only able to switch the rank and exports number?
You've been so much help - I'm sorry for being slow and troubling you so much.
|

January 27th, 2013, 08:45 PM
|
 |
Contributing User
|
|
|
|
Code:
>>> A = [('1,904', 'China', '1')]
>>> T = A[0]
>>> print(T)
('1,904', 'China', '1')
>>> print(T[1])
China
>>> print((T[1],T[0],T[2],))
('China', '1,904', '1')
>>>
Executable Iverson notation has an anagram verb that performs a specific rearrangement.
Code:
2 A. '1,904'; 'China'; '1'
┌─────┬─────┬─┐
│China│1,904│1│
└─────┴─────┴─┘
DATA =: _3[\'1'; 'China'; '1;904'; '2'; 'United States'; '1;497'; '3'; 'Germany'; '1;408'; '4'; 'Japan'; '788'; '5'; 'France'; '587.1'
DATA
┌─┬─────────────┬─────┐
│1│China │1;904│
├─┼─────────────┼─────┤
│2│United States│1;497│
├─┼─────────────┼─────┤
│3│Germany │1;408│
├─┼─────────────┼─────┤
│4│Japan │788 │
├─┼─────────────┼─────┤
│5│France │587.1│
└─┴─────────────┴─────┘
3 A."1 DATA NB. apply anagram on rank 1
┌─────────────┬─────┬─┐
│China │1;904│1│
├─────────────┼─────┼─┤
│United States│1;497│2│
├─────────────┼─────┼─┤
│Germany │1;408│3│
├─────────────┼─────┼─┤
│Japan │788 │4│
├─────────────┼─────┼─┤
│France │587.1│5│
└─────────────┴─────┴─┘
Last edited by b49P23TIvg : January 27th, 2013 at 08:52 PM.
|

January 27th, 2013, 09:25 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 27
Time spent in forums: 17 h 19 m 39 sec
Reputation Power: 0
|
|
|
Hmm, thank you for this, I really appreciate it.
But it doesn't seem to work because it instead reorders the parentheses sets when I have more than one. It works fine if it's only one ('1,904', 'China', '1').
I don't know anymore. I've been search google all day for more information but it seems like I don't understand a lot of the stuff. Python's a lot harder than I imagined it to be.
Is there another way to build a [[country, exports, rank]] list of lists from [[rank, country, exports]]?
Do you know anything about like using cmp and mycmp (calling a helper function) and setting item1, item2, item3?
|

January 28th, 2013, 02:17 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by taeBaby But it doesn't seem to work because it instead reorders the parentheses sets |
I think you need to (a) examine the differences between Python’s tuple, list, dictionary and set types, and (b) make sure what you are asking for is the right thing to do.
As for (b), IMHO it looks like there’s no need to reorder the data itself but just the way it is presented to the user, and the solution b49P23TIvg first gave is thus the correct one.
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|