Frank Harb
Frank Harb

Reputation: 79

Sorting dictionary data by value whilst retaining unique key

I've created a dictionary (largely through trial and error so I'm a bit lost) that looks like this:

{0: '68651880', 993793: '68656512', 29648: '68671002', 325637: '68656512', 282119: '68656512', 404490: '68656512', 3595: '68657355', 1549: '68655127', 559630: '68656512', 3599: '68657355', 3601: '68657355', 435730: '68656512', 241171: '68656512', 4118: '68657355', 439319: '68656512', 403480: '68656512', 349721: '68656512', 40476: '68660531', 337437: '68656512', 20510: '68671002', 4127: '68657355', 505888: '68656512', 459297: '68656512', 3419: '68660531', 73533: '68660531', 5201: '68651903', 169511: '68657355', 450689: '68656512', 22949: '68671002', 26631: '68671002', 16940: '68671002', 9774: '68651903', 50735: '68671002', 449073: '68656512', 374322: '68656512', 350263: '68656512', 2525: '68655127', 411195: '68656512', 237630: '68671002', 24639: '68671002', 1417: '68655127', 3649: '68657355', 404035: '68656512', 409161: '68656512', 330826: '68656512', 3659: '68657355', 20044: '68660531', 195661: '68671002', 67482: '68671002', 259151: '68656512', 3310: '68657355'}

To help you understand, the key (first value) is a meausrement of time in seconds, and the value (second value) are a bunch a job IDs which starts with 6, are all 8 digits long, and sometimes they repeat themselves.

I need to organise the data so that I can see the time in seconds put in ascending order for each job ID. So if the job ID repeats itself, as some of them do, each corresponding time in seconds for that job ID I would like to sort in ascending order.

Here is the code for how I generated the list of data if interested:

#create urlfriendlylist
for item in urlfriendlylist:
    fpp = FarmPageParser("http://farmweb/AnimalFarm/" + item + ".html#subjobs")
    fpp.reset()
    slowjobs = fpp.getFarmTableHTML(4)
    fpp.feed(slowjobs)
    x = fpp.table
    #turn back into jobid
    key = item[0:5] + item[6:9]
    #create dictionary of CPU time and jobid
    for a in x:
        try:
            CPUtime = a[2]
            #use function to convert hh:mm:ss into ssssss
            sec_got = get_sec(CPUtime)
            #populate dictionary with loop
            dic[sec_got] = key
        except: pass
print dic

Upvotes: 0

Views: 17

Answers (1)

trincot
trincot

Reputation: 350760

You could create another dict for that, as follows:

dic2 = {}
for key, value in dic.iteritems():
    if not value in dic2:
        dic2[value] = []
    dic2[value].append(key)

for lst in dic2:
    lst.sort()

print dic2

Upvotes: 2

Related Questions