2 个列表排序字典回到 2 个列表(多个键值)
2 Lists to Sorted Dictionary back to 2 Lists (Multiple Key Values)
我正在尝试获取两个列表:一个包含日期,一个包含分数,然后将它们压缩到字典中,然后按键值排序,然后将它们返回到两个列表中(仍然排序) .我遇到的问题是字典不保留多个键值。我的代码如下:
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])
但是,当我打印出这些列表时,长度仅为 24,而不是应该的 59。多个相同的密钥不会回来。我想知道是否有一种简单的方法可以将所有 59 个排序的元素返回到两个列表中。我查看了其他一些类似的 python 答案,但 none 对我有用。理想情况下,我不想为日期创建对象(除非这是最简单的方法),但是当我尝试这样做时,我不断出错。另外,我在 Python 2.7.
date, Scores = zip(*sorted(zip(date, Scores)))
字典不支持相同键的多个副本。您的密钥应该是唯一的。实现此目的的一种方法是使密钥成为您的日期和分数的元组,除非您在同一天获得相同的分数。在这种情况下,您可以添加编号为 0 到 58 的第三个列表,并将其用作第三个元素(使其成为三元组而不是元组)作为排序时的决胜局。
在这种情况下使用列表可能更有意义:
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)
输出是两个列表,第一个按日期排序(如您使用 YYYY/MM/DD),第二个列表按分数排序。我把它们放在一起。输出结果如下:
[('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
字典中的键是唯一的,因此您只能为每个日期捕获一个项目。不需要 dict...zip 将 return 一个你然后排序的元组。
z = zip(date, Scores)
sorted(z, key=lambda val: val[1], reverse=True)
我正在尝试获取两个列表:一个包含日期,一个包含分数,然后将它们压缩到字典中,然后按键值排序,然后将它们返回到两个列表中(仍然排序) .我遇到的问题是字典不保留多个键值。我的代码如下:
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])
但是,当我打印出这些列表时,长度仅为 24,而不是应该的 59。多个相同的密钥不会回来。我想知道是否有一种简单的方法可以将所有 59 个排序的元素返回到两个列表中。我查看了其他一些类似的 python 答案,但 none 对我有用。理想情况下,我不想为日期创建对象(除非这是最简单的方法),但是当我尝试这样做时,我不断出错。另外,我在 Python 2.7.
date, Scores = zip(*sorted(zip(date, Scores)))
字典不支持相同键的多个副本。您的密钥应该是唯一的。实现此目的的一种方法是使密钥成为您的日期和分数的元组,除非您在同一天获得相同的分数。在这种情况下,您可以添加编号为 0 到 58 的第三个列表,并将其用作第三个元素(使其成为三元组而不是元组)作为排序时的决胜局。
在这种情况下使用列表可能更有意义:
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)
输出是两个列表,第一个按日期排序(如您使用 YYYY/MM/DD),第二个列表按分数排序。我把它们放在一起。输出结果如下:
[('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
字典中的键是唯一的,因此您只能为每个日期捕获一个项目。不需要 dict...zip 将 return 一个你然后排序的元组。
z = zip(date, Scores)
sorted(z, key=lambda val: val[1], reverse=True)