0

I'd like to sort a list of tuples by the magnitude of a unicode string, i.e. the first member of the tuple.

The "raw" tuples look like this:

A

[

   [
        (u'90000', 100318), 
        (u'21000', 58094), 
        (u'50000', 14695), 
        (u'250000', 8190), 
        (u'100000', 5718), 
        (u'40000', 4276)
     ]
]

After processing I'd like it to look like this:

B

[

   [
        (u'250000', 8190), 
        (u'100000', 5718), 
        (u'90000', 100318),
        (u'50000', 14695), 
        (u'40000', 4276),
        (u'21000', 58094)
     ]
]

I've tried some different approaches but I've not been able to get it to persist.

How to sort the list of tuples in A to render it like B?

2
  • 3
    Did you just copy-paste an email or chat conversation or something into the question box? Commented Sep 17, 2017 at 0:24
  • I don't see how the linked question is related or more general. Here you're sorting a list of tuples, there data is being read from a list of dictionaries Commented Sep 17, 2017 at 0:26

2 Answers 2

1

Here is an interactive demonstration using list.sort().

>>> help(list.sort)
Help on built-in function sort:

sort(...)
    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    cmp(x, y) -> -1, 0, 1

The built-in help is useful but for a more complete explanation of the parameters we need to consult the python documentation

cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.

key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).

reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

Continuing on:

>>> l = [[(u'90000', 100318),(u'21000', 58094),(u'50000', 14695),(u'250000', 8190),(u'100000', 5718),(u'40000', 4276)]]
>>> l[0].sort(key=lambda x:int(x[0]),reverse=True)
>>> l
[[(u'250000', 8190), (u'100000', 5718), (u'90000', 100318), (u'50000', 14695), (u'40000', 4276), (u'21000', 58094)]]
Sign up to request clarification or add additional context in comments.

3 Comments

You could improve this answer by actually explaining how the code works and what a sort key is
Added doc link and excerpt
that's much better!
0

Due to inner list, you need to dig into next layer => A[0]

Then use sort function with key & reverse to help you achieve the goal

list.sort(A[0], key= lambda x: int(x[0]), reverse=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.