这是输出的片段:
innerHTML
我希望得到一个字典格式的变量pageIdCount,其中键是page_id,值是user_id发生的page_id的计数。因此对于userId 1,它应该看起来像:
{...,"resultMap":
{..."SEARCH_RESULTS":
[{..."resultList":[
{"userClientId":"1"","preferenceValues":["48","51","94"],"MyDate":"7/26/2017 8:30:00 AM"},
{"userClientId":"2","preferenceValues":["42","11","84"],"MyDate":"7/26/2017 9:40:00 AM"},
{"userClientId":"3","preferenceValues":["4","16","24"],"MyDate":"7/26/2017 4:20:00 PM"},
{"userClientId":"4","preferenceValues":["7","2","94"],"MyDate":"7/27/2017 8:00:00 AM"},
{"userClientId":"1","preferenceValues":["48","22","94"],"MyDate":"7/27/2017 1:50:00 PM"},
{"userClientId":"2","preferenceValues":["42","11"],"MyDate":"7/27/2017 2:00:00 PM"},
{"userClientId":"3","preferenceValues":["4","24"],"MyDate":"7/27/2017 6:15:00 PM"},
{"userClientId":"4","preferenceValues":"7","MyDate":"7/27/2017 9:30:00 PM"}]
}]
}
}
请注意,当preferenceValues中只有1个变量时,没有括号。还有一个字段" preferenceValue"无论什么都没有括号,它与" preferenceValues"相同否则。
这可能吗?
在Python 2.7中,我指定了用户,密码和网址,然后我有以下内容:
{"userClientId":"1","preferenceValues":{48:2, 51:1, 94:2, 22:1}}
非常感谢任何帮助。
答案 0 :(得分:1)
your_data # this is your data
final_data = {}
for line in yourdata:
uid = line["userId"]
pids = line["PageId"]
if uid not in final_data :
final_data[uid] = {}
for pid in pids :
pid = int(pid)
if pid not in final_data[uid]:
final_data[uid][pid]=0
final_data[uid][pid] += 1
res = [{"userId":uid,"PageIDCount":pids} for uid,pids in final_data.items()]
我想你是开始的,如果是这样的话,这段代码中最棘手的部分可能就是最后一行,它使用了列表理解。关于它,这里是a good lesson。