如果每个列表的第一个元素相同,如何组合两个列表中的所有参数

How to combine all arguments in two lists if the first element of each list is the same

setting = Subject.objects.annotate(A_setup=Count('id', filter=Q(type='A'), distinct=True) * Value(50),
                      B_setup=Count('id', filter=Q(type='B'), distinct=True) * Value(30),
                      C_setup=Count('id', filter=(~Q(type='A') & ~Q(type='B') & ~Q(type__isnull=True) & Q(id__in=workers.filter(worker=1).values('id')))) * Value(10)) \
            .values('setting__user_id', 'A_setup', 'B_setup', 'C_setup')

setting = [{'setting__user_id': 4, 'A_setting': 50.0, 'B_setting': 120, 'C_setting': 10.0}, {'setting__user_id': 34, 'A_setting': 0.0, 'B_setting': 0, 'C_setting': 0.0}, {'setting__user_id': 33, 'A_setting': 0.0, 'B_setting': 150, 'C_setting': 0.0}, {'setting__user_id': 30, 'A_setting': 0.0, 'B_setting': 150, 'C_setting': 0.0}, {'setting__user_id': 74, 'A_setting': 50.0, 'B_setting': 120, 'C_setting': 10.0}]


uploader = Feedback.objects           .values('uploader_id').distinct().values_list('uploader_id')
uploader = [{'uploader_id': 25}, {'uploader_id': 20}, {'uploader_id': 74}, {'uploader_id': 34}, {'uploader_id': 93}, {'uploader_id': 88}, {'uploader_id': 73}, {'uploader_id': 89}, {'uploader_id': 30}, {'uploader_id': 33}, {'uploader_id': 85}, {'uploader_id': 4}, {'uploader_id': 46}]

"设置"只输出满足条件的用户。但我需要所有用户的列表。 “上传者”是一个包含所有用户的查询集。首先,打印整个用户列表,如果用户的 id 包含在“设置”中,则输出设置值。我最终想要达到的效果如下

Desired Result: [['25', '0', '0', '0'], ['20', '0', '0', '0'], ['74', '50', '120', '10'], ['34', '0', '0', '0'], ['93', '0', '0', '0'], ['88', '0', '0', '0'], ['73', '0', '0', '0'], ['89', '0', '0', '0'], ['30', '0', '150', '0'], ['33', '0', '150', '0'], ['35', '0', '0', '0'], ['4', '50', '120', '10'], ['46', '0', '0', '0']]

对不起。请再次检查。有两个查询集。我想获取setting的A_setup、B_setup、C_setup值如果uploader id和setting的uploader id展开uploader queryset后相同,有则加0设置中没有上传者 ID。 如何获得我想要的结果?

在我看来,最好稍微改变一下数据结构(变成下面的setting_newuploader_ids),然后使用列表理解:

from operator import itemgetter

# setting = [{'setting__user_id': 4, 'A_setting': 50.0, 'B_setting': 120, 'C_setting': 10.0}, ...
# uploader = [{'uploader_id': 25}, {'uploader_id': 20}, {'uploader_id': 74}, ...

setting_new = {dct['setting__user_id']: itemgetter('A_setting', 'B_setting', 'C_setting')(dct) for dct in setting}
uploader_ids = map(itemgetter('uploader_id'), uploader)

output = [[i, *setting_new.get(i, (0,0,0))] for i in uploader_ids]
print(output)
# [[25, 0, 0, 0], [20, 0, 0, 0], [74, 50.0, 120, 10.0], [34, 0.0, 0, 0.0], ...