将多个值添加到字典然后对其进行排序

时间:2015-05-08 14:39:34

标签: python dictionary sorted

我有一个包含三个属性的文件:id,text和date。此文件中大约有70K记录。我希望将这些数据添加到字典中,然后按日期对其进行排序。下面是代码。

matchinput = csv.reader(open(filename,"rb"),delimiter=',', quotechar='|')
tweets = []
for row in matchinput:
    data = dict()
    data['id']=str(row[0])
    data['text']=str(row[1])
    data['date']=str(row[2])
    tweets.append(data)

sorted(tweets, key=lambda tweets: tweets[2])
print tweets

代码显示以下错误:

sorted(tweets, key=lambda tweets: tweets[2])
KeyError: 2

输入文件:

566561942949474304,"lala is only 52 runs and 7 wickets away from being the only player to score 8000 runs and take 400 wickets in odi's !!! #pakvsind #cwc15",2015-02-14 22:37:48
566561925178200064,"rt @shoaibakhtarpk: captain @misbahulhaqpk, speaking to media, says want to make history by wining match against india #cwc15#pakvind #ind",2015-02-14 22:37:43

输出文件:

566561925178200064,"rt @shoaibakhtarpk: captain @misbahulhaqpk, speaking to media, says want to make history by wining match against india #cwc15#pakvind #ind",2015-02-14 22:37:43
566561942949474304,"lala is only 52 runs and 7 wickets away from being the only player to score 8000 runs and take 400 wickets in odi's !!! #pakvsind #cwc15",2015-02-14 22:37:48

1 个答案:

答案 0 :(得分:1)

为什么不将每行存储为list / tuple,因为您在解析csv文件时已经假设row[0] = idrow[1] = textrow[2] = date 。这样,每个id / text / date组合都保持在一起:

# to take care of any fileio cleanup and clean unnecessary lines
with open(filename, 'rb') as csvfile:
    data = [row for row in csv.reader(csvfile, delimiter=',', quotechar='|')
sorted_data = sorted(data, key=lambda t: t[-1]) # or t[2]

如果您希望将ID,文本和日期分开,则可以使用zip

ids, texts, dates = zip(*sorted_data)

编辑:反映您对日期的关注,示例代码中的字符串格式应正确排序为字符串。但是,更一般地说,您可以始终执行以下操作以确保正确排序任何日期/时间格式(我使用了与您当前日期时间格式对应的strptime字符串)。

import datetime
date_key = lambda t: datetime.datetime.strptime(t[-1], '%Y-%m-%d %H:%M:%S')
sorted_data = sorted(data, key=date_key)