2列表到排序字典回到2列表(多个键值) [英] 2 Lists to Sorted Dictionary back to 2 Lists (Multiple Key Values)

查看:184
本文介绍了2列表到排序字典回到2列表(多个键值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拿两个列表:一个包含日期和一个包含分数,然后将它们压缩到一个字典,然后按键值排序,然后将它们转回两个列表(仍然排序)。我遇到的问题是字典不保留多个键值。我的代码如下:

  date = ['2015/07/13','2015/07/13' 2015/07/07','2015/07/06',...] 
#这些日期中有59个
分数= [9.5,13.9,15.5,12.9 ....]这些分数中有59个
dictionary = dict(zip(date,Scores))
d = sorted(dictionary.items())
dte = []
scr = []
在d中:
dte.append(i [0])
scr.append(i [1])$ ​​b $ b

但是,当我打印出这些列表时,长度只有24,而不是59。多个相同的键不会退出。我想知道是否有一个简单的方法来获得所有59个排序的元素回到两个列表。我看了一些类似的其他python答案,但没有一个为我工作。理想情况下,我不想为日期创建对象(除非是最简单的方法),但是当我尝试这样做时,我会收到错误。另外,我在Python 2.7中。

解决方案

  date,Scores = zip (zip(日期,分数)))


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)))

这篇关于2列表到排序字典回到2列表(多个键值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆