#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    27
    Rep 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...
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    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 and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    27
    Rep Power
    0
    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
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    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')]
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    27
    Rep 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.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    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 09:52 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    27
    Rep 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?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    439
    Rep Power
    67
    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 13.2 (home desktop, work desktop), openSUSE 13.1 (home laptop), Debian GNU/Linux 7.7.0 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)

IMN logo majestic logo threadwatch logo seochat tools logo