Tuesday, 15 April 2014

python - Data sorting within lists within dictionaries within files -


Although I searched around, I did not find specific coding related to my program.

I have a program that loads into a file that is used for pickling, the data is stored as a variable, there is a dictionary with values ​​in the data list format. I have been assigned a task to sort the dictionary with the highest score for each key and then from highest score to highest

apart from this, I have to present my findings in a user-friendly way, so Climbing the loop may be necessary.

You can create an array from the list with numpy.

or manual:

  Numpy import asarray myarray = asarray (mylist)  

and then sort ()

or manual:

K., V. for
  dict = {'3': 'AC', '2': 'FAS', '4': 'ABA'] ARR = [] In. Dictitems (): arr.append ([k, v])  

and then arr.sort () if its one-dimensional, or

< Pre> print sorted (arr, key = lambda x: x [0], reverse = true)

if its multi-functional

Edit After discussions in comments:

Do you need it? [2, 3, 1], 'john': [3, 2, 4], '' [1, 4, 2], 'Steve': ' Thomas': [4,1, 3]} for arr = [] dict.iteritems () in k, v: arr.append ([k, v [0], v [1], v [2]]) Print sorted (arr, key = lambda y: y [1]) # Sorted from first number (arr, key = lambda y: y [2]) # Second number printed sorted (arr, key = Lambda y: y [3]) # After the discussion of the third number,

EDIT2

This is fine for me Most doubt, and I hope some dragon gurus will improve this. [3, 2, 4], 'Thomas': [1, 4, 2], 'Steve': [2, 3], 'John': [3, 2, 4] : [4,1, 3]} for arr = [] k, in dict.iteritems (V): new = [k] # add I in the list for all values ​​(0, Lane (V)): new .append (v [i]) # If a list is small then 4 items, add it to add zero, while len (new) & lt; 4: new.append (0) # main list in the new list arr.append (new) print arr print sorted (arr, key = lambda y: y [1]) # printed from the first number (arr, Key = lambda y: y [2]) # Second number printed sorted (arr, key = lambda y: y [3]) # By third number

0 if you want empty people on the other end of the sort, then from time to time 999 in the loop.

Hopefully this will complete it, though this file may be a little late.


No comments:

Post a Comment