How to create a list based on same value of a dictionary key

六月ゝ 毕业季﹏ 提交于 2019-12-24 08:59:30

问题


I am trying to join together dictionaries that contain the same date, and also create a list of the temperature values that these common dates have to then pull the max and min of these values.

I have this:

data = 
[{'temp_min': 51.75, 'date': '2019-05-31', 'temp_max': 52.25}, 
 {'temp_min': 52.5, 'date': '2019-05-31', 'temp_max': 52.87}, 
 {'temp_min': 53.29, 'date': '2019-05-31', 'temp_max': 53.55}, 
 {'temp_min': 68.19, 'date': '2019-06-01', 'temp_max': 75.19}, 
 {'temp_min': 61.45, 'date': '2019-06-01', 'temp_max': 68.45}, 
 {'temp_min': 56.77, 'date': '2019-06-01', 'temp_max': 59.77}]

And want this:

[{'date':'2019:05-31', 'temp_min':[51.75, 52.5, 53.29], 'temp_max': 
[52.25, 52.87, 53.55]}, {'date':'2019:06-01','temp_min':[68.19, 
 61.45, 56.77], 'temp_max':[75.19, 68.45, 59.77]}]

I am trying to do this using itertools groupby but am getting stuck when I try to create the output as mentioned above. If there is a different approach to this that is also welcome. I wasn't sure how to get the groupings back into a dictionary and also keep the unique date.

def get_temp(temp):
    return temp['date']

grouping = itertools.groupby(data, get_temp)

for key, group in grouping:
    print(key)
        for d in group:
            print(d['temp_max'])

回答1:


Iterate over group to sort out mins and maxs to separate keys of the dictionary:

def get_temp(temp):
    return temp['date']

lst = []
for key, group in itertools.groupby(data, get_temp):
    groups = list(group)
    d = {}
    d['date'] = key
    d['temp_min'] = [x['temp_min'] for x in groups]
    d['temp_max'] = [x['temp_max'] for x in groups]
    lst.append(d)

print(lst)



回答2:


You can use defaultdicts to build the lists and then list comprehension to reconstruct the list of dictionaries:

from collections import defaultdict
mx = defaultdict(list)
mn = defaultdict(list)
for d in data:
  mx[d['date']].append(d['temp_max'])
  mn[d['date']].append(d['temp_min'])

[{'date': k, 'temp_min': mn[k], 'temp_max': mx[k]} for k in mx]
#[{'date': '2019-05-31', 'temp_min': [51.75, 52.5, 53.29], 
# 'temp_max': [52.25, 52.87, 53.55]}, {'date': '2019-06-01',
# 'temp_min': [68.19, 61.45, 56.77], 'temp_max': 
#  [75.19, 68.45, 59.77]}]



回答3:


You might be more successful sticking to a dictionary format:

new_data = {}
for record in data:
  if record['date'] not in new_data.keys():
    new_data[record['date']]={'temp_max':[], 'temp_min' : []}
  # append values
  new_data[record['date']]['temp_max'].append(record['temp_max'])
  new_data[record['date']]['temp_min'].append(record['temp_min'])

Alternatively, you can do this same manipulation in pandas:

df = pd.DataFrame(data)

new_data = []
for date in df.date.unique():
  df_temp = df[df.date == date]
  temp_max = list(df_temp.temp_max)
  temp_min = list(df_temp.temp_min)

  new_data.append({'date':date, 'temp_max':temp_max, 'temp_min':temp_min})

As a side note, it would be helpful to know what you were using this manipulation for so as best to create something useful for your larger use case.




回答4:


Just to show you what I meant in my comment by aiming for a dict of dicts instead of a list of dicts:

from collections import defaultdict
newdict = defaultdict(dict)

for d in data:
    newdict[d['date']]['Tmin'] = newdict[d['date']].get('Tmin', []) + [d['temp_min']]
    newdict[d['date']]['Tmax'] = newdict[d['date']].get('Tmax', []) + [d['temp_max']]

# defaultdict(<class 'dict'>, {'2019-05-31': {'Tmin': [51.75, 52.5, 53.29], 'Tmax': [52.25, 52.87, 53.55]}, '2019-06-01': {'Tmin': [68.19, 61.45, 56.77], 'Tmax': [75.19, 68.45, 59.77]}})   

This would have the advantage that you don't have to search a list at which index which date is stored.
You could easily do sth like

newdict['2019-06-01']['Tmin']

and would receive all the Tmin data of the first of June:

[68.19, 61.45, 56.77]


来源:https://stackoverflow.com/questions/56412904/how-to-create-a-list-based-on-same-value-of-a-dictionary-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!