Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 27th, 2013, 05:01 PM
taeBaby taeBaby is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 27 taeBaby User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #2  
Old January 27th, 2013, 05:32 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old January 27th, 2013, 06:15 PM
taeBaby taeBaby is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 27 taeBaby User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old January 27th, 2013, 07:43 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
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')]
>>> 

Reply With Quote
  #5  
Old January 27th, 2013, 08:08 PM
taeBaby taeBaby is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 27 taeBaby User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old January 27th, 2013, 08:45 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
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.

Reply With Quote
  #7  
Old January 27th, 2013, 09:25 PM
taeBaby taeBaby is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 27 taeBaby User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #8  
Old January 28th, 2013, 02:17 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 403 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 54 sec
Reputation Power: 65
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)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Reordering value?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap