I have the following list of dicts:
authorvals= [
{
"author": "author1",
"year": [
"2016"
],
"value1": 4.0
},
{
"author": "author2",
"year": [
"2016"
],
"value1": 2.0
},
{
"author": "author1",
"year": [
"2016"
],
"value3": 1.0
},
{
"author": "author1",
"year": [
"2016"
],
"value2": 4.0
},
{
"author": "author2",
"year": [
"2016"
],
"value2": 2.0
}]
Now I want lists from the dict as follows:
val_list=["value1","value2","value3"]
num_list=[[4,2],[4,2],[1,0]]
auth_list=["author1","author2"]
I want the dict as three separate lists.
- First list is the keys "value"+x in the dict
- Second list is the value of that particular key for auth1 and auth2
- Third list is just the list of authors
I have tried the following code:
num_list=[]
auth_list=[]
val_list=[]
for item in authors_dict:
if item['author'] not in auth_list:
auth_list.append(item['author'])
for k in item.keys():
if k.startswith("value") and k not in val_list:
val_list.append(k)
val_list.sort()
for v in val_list:
temp_val_list = []
for i in authors_dict:
try:
val = i[v]
temp_val_list.append(val)
except:
pass
if len(temp_val_list) > 0:
num_list.append(temp_val_list)
print(val_list)
print(num_list)
print(auth_list)
but this is not what I want to accomplish the 0 in the last list of num_list is because there is no value for author2.If there is no value,then 0 should be printed