【问题标题】:Sorting dictionary data by value whilst retaining unique key按值对字典数据进行排序,同时保留唯一键
【发布时间】:2017-08-23 21:59:57
【问题描述】:

我创建了一个字典(主要是通过反复试验,所以我有点迷茫),看起来像这样:

{0:'68651880',993793:'68656512',29648:'68671002',325637:'68656512',282119:282119:'68656512'68656512',4044490:'68655512512,512,, '6865'',3.65'','65.65'',3'''''''''''''''' ', 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 ',449073:'68656512',374322:'68656512',350263:'68656512',2525',2525:'68655127',411195:'68656512'68656512',237630:'686171,471,000,000,000,000,000,000,000,000,000,000,,,,, 3649: '68657355', 404035: '68656512', 409161: '68656512', 330826: '68656512', 3659: '68657355', 20044: '68660531', 195661: '68671002', 67482: '68671002', 259151: '68656512', 3310: '68657355'}

为了帮助您理解,键(第一个值)是以秒为单位的时间测量值,值(第二个值)是一堆以 6 开头的作业 ID,长度均为 8 位,有时它们会重复自己。

我需要组织数据,以便我可以查看按升序排列的每个作业 ID 的时间(以秒为单位)。因此,如果作业 ID 重复自身,就像他们中的一些人所做的那样,该作业 ID 的每个相应时间(以秒为单位)我想按升序排序。

如果有兴趣,这里是我如何生成数据列表的代码:

#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

【问题讨论】:

标签: python-2.7 sorting dictionary


【解决方案1】:

您可以为此创建另一个字典,如下所示:

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

【讨论】:

  • 从技术上讲,他希望时间按升序排序,但除此之外这看起来不错
  • 非常感谢!是的,这几乎是完美的。现在我只需要罗比琼斯所说的那些时间值上升。
  • 添加了一个循环排序
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 2011-07-08
  • 2021-04-14
  • 1970-01-01
相关资源
最近更新 更多