简体   繁体   中英

2 Lists to Sorted Dictionary back to 2 Lists (Multiple Key Values)

I am trying to take two lists: one that contains dates and one that contains scores, then zip them into a dictionary, then sort by the key value, then turn them back in two lists (which are still sorted). The problem that I am encountering is that the dictionary does not keep multiple key values. My code is as follows:

date = ['2015/07/13', '2015/07/13', '2015/07/07', '2015/07/06',...] 
#there are 59 of these dates
Scores = [9.5, 13.9, 15.5, 12.9 .... ] #There are 59 of these scores
dictionary = dict(zip(date, Scores)) 
d = sorted(dictionary.items()) 
dte = []
scr = []
for i in d: 
    dte.append(i[0]) 
    scr.append(i[1])

However, when I print out these lists the length is only 24 in length instead of 59, which it should be. The multiple same keys are not coming back out. I was wondering if there was an easy way to get all 59 sorted elements back in two lists. I looked at some of the other python answers that were sort of similar, but none had worked for me. Ideally, I would not like to create objects for the dates (unless that is the easiest method), however when I tried to do so I kept getting errors. Also, I am on Python 2.7.

date, Scores = zip(*sorted(zip(date, Scores)))

Dictionaries do not support multiple copies of identical keys. Your keys should be unique. One way to accomplish this is to make the key a tuple of your dates and scores unless you got the same score on the same day. In which case you could add a third list numbered 0 to 58 and use that as the third element (make it a triple instead of a tuple) as a tiebreaker when sorting.

Working with lists in this situation might make more sense:

dates = ['2015/07/13', '2015/07/13', '2015/07/07', '2015/07/06'] 
scores = [9.5, 13.9, 15.5, 12.9] 

ldates_scores = zip(dates, scores)

dte = sorted(ldates_scores)
scr = sorted(ldates_scores, key=lambda x: x[1])

print dte
print scr
print

for date,score in dte:
    print "Date: %-10s   Score: %d" % (date, score)

print

for date,score in scr:
    print "Date: %-10s   Score: %d" % (date, score)

The output is two lists, sorted first by date (as you are using YYYY/MM/DD), and the second list sorted by score. I have kept the pairs together. The output is as follows:

[('2015/07/06', 12.9), ('2015/07/07', 15.5), ('2015/07/13', 9.5), ('2015/07/13', 13.9)]
[('2015/07/13', 9.5), ('2015/07/06', 12.9), ('2015/07/13', 13.9), ('2015/07/07', 15.5)]

Date: 2015/07/06   Score: 12
Date: 2015/07/07   Score: 15
Date: 2015/07/13   Score: 9
Date: 2015/07/13   Score: 13

Date: 2015/07/13   Score: 9
Date: 2015/07/06   Score: 12
Date: 2015/07/13   Score: 13
Date: 2015/07/07   Score: 15

The keys in a dict will be unique so you are only capturing a single item for each date. No need for the dict...zip will return a tuple which you then sort.

 z = zip(date, Scores)
 sorted(z, key=lambda val: val[1], reverse=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM