【发布时间】:2012-04-12 21:46:51
【问题描述】:
可能重复:
How do I make a dictionary with multiple keys to one value?
我有 5 列数据,
usermac, useragent, area ,videoid, number of requests
我想同时使用 (usermac,useragent) 作为键来创建字典,因为 (usermac, useragent) 的唯一组合代表一个唯一用户。
所以字典会是这样的:
usermac1, useragent1: area1, videoid1, 10
area1, videoid2, 29
usermac1, useragent2: area1, videoid1, 90
area1, videoid2, 34
...
我只知道如何创建一个只有一项作为键的字典。那么有人可以帮忙吗?
我的代码是:
for line in fd_in.readlines():
(mac, useragent, area, videoid, reqs) = line.split()
video_dict = d1.setdefault((mac,useragent) {})
video_dict.setdefault(videoid, []).append(float(reqs))
它有语法错误:
video_dict = d1.setdefault((mac,useragent) {})
【问题讨论】:
-
@jamylak 也许我应该添加我的代码,我的代码中有一些语法错误,所以我很感激你的帮助,谢谢
标签: python