python - Convert string to dictionary with counts of variables -


this snippet of output:

           {...,"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"}]              }]             }            } 

i looking variable pageidcount in dictionary format, key page_id , values counts of occurrences of page_id, user_id. userid 1 should like:

{"userclientid":"1","preferencevalues":{48:2, 51:1, 94:2, 22:1}} 

note when there 1 variable inside preferencevalues- there no brackets. there field "preferencevalue" there no brackets no matter , identical "preferencevalues" otherwise.

is possible?

in python 2.7, specify user, password , url , have following:

req = requests.post(url = url, auth=(user, password)) ans = req.json()  print ["resultmap"]["search_results"][0]["resultlist"] 

any appreciated.

your_data # data final_data = {} line in yourdata:     uid = line["userid"]     pids = line["pageid"]     if uid not in final_data :         final_data[uid] = {}     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} uid,pids in final_data.items()] 

i suppose beginning, if so, tricky part of code last line, uses list comprehension. here a lesson it.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -