0

I need some help with sorting in python. I got a data object, in this objects are lists and I like to sort the data for a specific list item. Currently I print it out like this:

 for item in data:
     for proj in item:
         print proj.get('id')

So I want this data already sorted by the 'id' item.

This is how the data object looks like, if I print it:

[[{u'archived': False, u'name': u'someone', u'num_files': 0, u'managed_by': {u'username': u'somebody', u'role': u'somerole', u'email_address': u'me@somewhere', u'id': u'307', u'name': u'firstname lastname'}, u'updated_on': u'2015-06-18 17:55:39', u'id': 23}, 
{u'archived': False, u'name': u'someoneelse', u'num_files': 0, u'managed_by': {u'username': u'somebody else', u'role': u'somerole', u'email_address': u'you@somewhere', u'id': u'341', u'name': u'Firstname Lastname'}, u'updated_on': u'2015-06-09 17:38:52', u'id': 48}]]

Just two lists from the whole, but I like to sort for the last id, in this example 23 and 48

5
  • 3
    Can you show an example of the data, and an example of it sorted? Commented Jun 19, 2015 at 11:58
  • Just sort the lists with list.sort Commented Jun 19, 2015 at 11:59
  • just added an data example, if I just use data.sort I get TypeError: 'builtin_function_or_method' object is not iterable Commented Jun 19, 2015 at 12:08
  • Can you give an example which has more than one data object, and each data object has more than one item? What would the output look like if you had two data objects, one that contained ids 42 and 15, and the other containing ids 23 and 8? Commented Jun 19, 2015 at 12:15
  • The example object has two lists, in reality it hast about 40 lists. I just need the last id as it is an item in that list. The other id in this line belong to another sublist in that list. - by the way, I'm not sure if I'm using the terms for lists objects and items etc correct ... I'm kinda new to python Commented Jun 19, 2015 at 12:26

2 Answers 2

1

Try this:

import numpy as np
data_new = np.array(data).flatten()
sorted_list = sorted(data_new, key=lambda x: x['id'])

First line will create one list of dict. Second will sort them by 'id' key. After that you could print it like you want:

for i in sorted_list:
    print(i['id'])

Edit: Because your list could contain sublists of different sizes (like here) this will broke ndarray.flatten or numpy.ravel, also because your elements are dictionaries np.fromiter will unworkable too. So you should change second line to this:

data_new = np.hstack(data)
Sign up to request clarification or add additional context in comments.

2 Comments

what do I have to import for np? Google said "import numpy as np" but I get "ImportError: No module named numpy"
yes, sorry, you should install and import numpy module (it's very usefull and popular).
0

I did a mistake, I used .append with my list to add more entries, instead I should have used .extend Now my list is not that deep anymore and it's all way much easier:

sorted_list = sorted(data, key=lambda x: x['id'])
for item in sorted_list:
    print str(item.get('id')) + " : " + item.get('name')

But thanks for the numpy tip, I think that would be helpful if I reall would search deeper in the objects sublist etc.

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.