我正在尝试创建具有多个字典(键,值)对的列表。例如,如果有一个学生并且他在不同的时间学习,那么我必须将不同的时间表合并为一个列表。我正在从数据库中提取数据。
获取的值如下:
{'name':'a', 'study_time':{'start':9,'end':10}}
{'name':'a', 'study_time':{'start':11,'end':12}}
{'name':'a', 'study_time':{'start':13,'end':15}}
{'name':'a', 'study_time':{'start':16,'end':19}}
{'name':'a', 'study_time':{'start':20,'end':21}}
我需要的是
{
'name':'a',
'study_time':[
{'start':9,'end':10},
{'start':11,'end':12},
{'start':13,'end':15},
{'start':16,'start':19},
],
}
我得到的是
{
'name':'a',
'study_time': [
[
[
{'start':9,'end':10},
{'start':11,'end':12},
],
{'start':13,'end':15},
{'start':16,'start':19},
],
{'start':20,'end':21},
],
}
我尝试过的是
parsed_user_details=[]
if len(parsed_user_details) == 0:
parsed_user_details.append(parsed_user_detail)
print("I am inside if", parsed_user_details,file=sys.stderr)
else:
parsed_user_details[0]['time'] = [
parsed_user_details[0]['time'],
parsed_user_detail['time'],
]
print("I am inside else", parsed_user_details)
总体上我尝试过的是,如果时间列表为0,只需将值添加到列表中,否则将值添加到列表中。 我解析的用户详细信息是 parsed_user_detail = {'name':'a','study_time':{'start':9,'end':10}} 每当我循环播放时,我都会一一得到下面的parsed_user_detail。
{'name':'a', 'study_time':{'start':9,'end':10}}
{'name':'a', 'study_time':{'start':11,'end':12}}
{'name':'a', 'study_time':{'start':13,'end':15}}
{'name':'a', 'study_time':{'start':16,'end':19}}
{'name':'a', 'study_time':{'start':20,'end':21}}
答案 0 :(得分:1)
您很近:
list_dict = [{'name':'a',"study_time":{'start':9,'end':10}},
{'name':'a',"study_time":{'start':11,'end':12}},
{'name':'a',"study_time":{'start':13,'end':15}},
{'name':'a',"study_time":{'start':16,'end':19}}]
name = list_dict[0]["name"]
parsed_user_details = []
for i in list_dict :
if len(parsed_user_details) == 0:
parsed_user_details.append(i)
parsed_user_details[0]["study_time"] = [parsed_user_details[0]["study_time"]]
else :
parsed_user_details[0]['study_time'].append(i['study_time'])
print(parsed_user_details)
您需要添加它,使第一个study_time成为一个数组,然后追加到它:
parsed_user_details[0]["study_time"] = [parsed_user_details[0]["study_time"]]
输出:
{'name': 'a',
'study_time': [{'end': 10, 'start': 9},
{'end': 12, 'start': 11},
{'end': 15, 'start': 13},
{'end': 19, 'start': 16}]}
答案 1 :(得分:1)
您可以使用[Python 3.Docs]: itertools.groupby(iterable, key=None)。我还添加了其他用户记录以增加复杂性:
public Bitmap mergeMultipleImages(Bitmap[] img_bitmaps){ Bitmap result = Bitmap.createBitmap(img_bitmaps[0].getWidth() * 2, img_bitmaps[0].getHeight() * 2, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(result); Paint paint = new Paint(); for (int i = 0; i < img_bitmaps.length; i++) { canvas.drawBitmap(img_bitmaps[i], img_bitmaps[i].getWidth() * (i % 2), img_bitmaps[i].getHeight() * (i / 2), paint); } return result; }
答案 2 :(得分:0)
所以我有一种解决方案,有点round回,但可以得到您的结果。 我假设您实际上可能有不同的名称,并且每个名称都想要一个对象?
from collections import defaultdict
l1 = [{'name':'a', 'study_time':{'start':9,'end':10}},
{'name':'a', 'study_time':{'start':11,'end':12}},
{'name':'a', 'study_time':{'start':13,'end':15}},
{'name':'a', 'study_time':{'start':16,'end':19}},
{'name':'a', 'study_time':{'start':20,'end':21}},]
dict_holder = defaultdict(list)
for d in l1:
dict_holder[d['name']].append(d['study_time'])
new_dicts = []
for name, study_list in dict_holder.items():
new_dicts.append({'name': name, 'study_time': study_list})
new_dicts
[{
'name': 'a',
'study_time': [
{'start': 9, 'end': 10},
{'start': 11, 'end': 12},
{'start': 13, 'end': 15},
{'start': 16, 'end': 19},
{'start': 20, 'end': 21}]
}]
答案 3 :(得分:0)
对于仅内置解决方案,请尝试以下操作:
data = [
{'name':'a', 'study_time':{'start':9,'end':10}},
{'name':'a', 'study_time':{'start':11,'end':12}},
{'name':'a', 'study_time':{'start':13,'end':15}},
{'name':'a', 'study_time':{'start':16,'end':19}},
{'name':'a', 'study_time':{'start':20,'end':21}}
]
result = []
for entry in data:
item = next(
(x for x in result if x.get('name') == entry.get('name')),
None
)
if not item:
item = {
'name': entry.get('name'),
'study_time': []
}
result.append(item)
item['study_time'].append(entry.get('study_time'))
答案 4 :(得分:0)
如果预期结果是多个词典的列表,则可以执行此操作。
from itertools import groupby
def get_studetails(student):
res = {}
for i in student:
if not res.get('name'):
res['name'] = i.get('name')
if not res.get('study_time') or res.get('study_time') == []:
res['study_time'] = []
res['study_time'].append(i.get('study_time'))
else:
temp_study = res.get('study_time')
temp_study.append(i.get('study_time'))
res['study_time'] = temp_study
return res
total_list = []
studentdetailsl = [{'name':'a','study_time':{'start':9,'end':10}},{'name':'a','study_time':{'start':11,'end':12}},{'name':'a','study_time':{'start':13,'end':15}},{'name':'a','study_time':{'start':16,'end':19}},{'name':'a','study_time':{'start':20,'end':21}}, {'name':'b','study_time':{'start':20,'end':21}},{'name':'b','study_time':{'start':11,'end':12}}]
for k,v in groupby(studentdetailsl, lambda x: x['name']):
total_list.append(get_studetails(list(v)))
from pprint import pprint
pprint(total_list)
输出:
[{'name': 'a',
'study_time': [{'end': 10, 'start': 9},
{'end': 12, 'start': 11},
{'end': 15, 'start': 13},
{'end': 19, 'start': 16},
{'end': 21, 'start': 20}]},
{'name': 'b',
'study_time': [{'end': 21, 'start': 20}, {'end': 12, 'start': 11}]}]