[Solved] Convert string to dictionary with counts of variables [closed]


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

I suppose you are beginning, if so, the most tricky part of this code will probably be the last line, it uses list comprehension. here is a good lesson about it.

1

solved Convert string to dictionary with counts of variables [closed]