简体   繁体   English

将字典列表转换为值字典

[英]Transforming list of dict to dict of values

I have a list of dictionaries for different attributes in the form 我有以下形式的不同属性的字典列表

attribute = [{user_id1:Value},{user_id2:Value};{User_id3:value3}]

these are my list 这些是我的清单

a = [{6: 81}, {7: 79}, {8: 67}]
b = [{6: 68}, {7: 77}, {8: 71}]
c = [{6: 71}, {7: 86}, {8: 68}]
d = [{6: 71}, {7: 86}, {8: 68}]
e = [{6: 67}, {7: 59}, {8: 85}]
f = [{6: -72}, {7: -71}, {8: -66}]

What I would need to do is to create a list a value for each user for example: 我需要做的是为每个用户创建一个值列表,例如:

list_user_id_6 = [81,68,71,71,67,-72]
list_user_id_7 = [79,77,86,86,59,-71]
list_user_id_8 = [67,71,68,68,85,-66]

edit: To explain what I did, I have a survey django app, and I am using Chart.JS to render the results, which have to be in the form : a = [number1,number2,number3] 编辑:为了解释我做了什么,我有一个调查django应用程序,并且我使用Chart.JS呈现结果,该结果的格式必须为:a = [number1,number2,number3]

Now those numbers are a calculation from multiple users values that are part of a team, so I created a method that for each member of a particular team I extract some value make a calculation to give me a score and then I append that score in a list with the member_id in the form {id:score} 现在,这些数字是团队中多个用户值的计算所得,因此我创建了一种方法,针对特定团队中的每个成员,我提取一些值进行计算以得到一个分数,然后将该分数附加到以{id:score}形式列出member_id

each a,b,c,d has a calculation method to create a score 每个a,b,c,d都有一种计算方法来创建分数

this is an example of one of the method: 这是该方法之一的示例:

def get_chunk_score2(self, format=None, *args, **kwargs):
    current_response_list = get_current_team(self)
    chunk_list = []
    for resp in current_response_list:
        current_response = list(resp.values())[0]

        answer_question1 = current_response.answers.get(question_id = 2)
        answer_question2 = current_response.answers.get(question_id = 3)
        json_answer_question1 = json.loads(answer_question1.body)
        json_answer_question2 = json.loads(answer_question2.body)
        answer_key_question1 = list(json_answer_question1.keys())[0][0]
        answer_key_question2 = list(json_answer_question2.keys())[0][0]
        if answer_key_question1 == "1" or "3":
            score1 = list(json_answer_question1.values())[0]
        else:
            score1 = -list(json_answer_question1.values())[0]

        if answer_key_question2 == "1" or "3":
            score2 = list(json_answer_question2.values())[0]
        else:
            score2 = -list(json_answer_question2.values())[0]

        chunk_score = math.ceil((score1+score2)/2)
        chunk_list.append({current_response.user_id:chunk_score})

    return chunk_list

ps: like you can see I am a few month in the world of coding so any tips is welcome to progress;) ps:就像您看到的那样,我在编码领域工作了几个月,因此欢迎您提出任何提示;)

how can I properly do it ? 我该怎么做呢? thx you very much 非常感谢你

You may want to store your list in a new dict: 您可能希望将列表存储在新字典中:

a = [{6: 81}, {7: 79}, {8: 67}]
b = [{6: 68}, {7: 77}, {8: 71}]
c = [{6: 71}, {7: 86}, {8: 68}]
d = [{6: 71}, {7: 86}, {8: 68}]
e = [{6: 67}, {7: 59}, {8: 85}]
f = [{6: -72}, {7: -71}, {8: -66}]

res = {}

group = [a,b,c,d,e,f]

for a in group:
    for d in a:
        # If actual key is already in dict, get his list, else create an empty list
        res[d.keys()[0]] = res.get(d.keys()[0], [])
        # append to list the value
        res[d.keys()[0]].append(d.values()[0])

print res
# {8: [67, 71, 68, 68, 85, -66], 6: [81, 68, 71, 71, 67, -72], 7: [79, 77, 86, 86, 59, -71]}

First convert your lists of dicts into a better data structure like a dictionary: 首先将字典列表转换为更好的数据结构,例如字典:

lists = [a, b, c, d, e, f]
dictionaries = [dict(k.items()[0] for k in x) for x in lists]

Now get all of the possible keys: 现在获取所有可能的密钥:

keys = {y for y in x.keys() for x in dictionaries}

And finally forms the lists for each of the keys: 最后形成每个键的列表:

result = {k: [d[k] for d in dictionaries] for k in keys}

You can try with two methods : 您可以尝试以下两种方法:

Data is : 数据是:

a = [{6: 81}, {7: 79}, {8: 67}]
b = [{6: 68}, {7: 77}, {8: 71}]
c = [{6: 71}, {7: 86}, {8: 68}]
d = [{6: 71}, {7: 86}, {8: 68}]
e = [{6: 67}, {7: 59}, {8: 85}]
f = [{6: -72}, {7: -71}, {8: -66}]

group = [a,b,c,d,e,f]

First method: 第一种方法:

one is using default dict: 一种是使用默认字典:

import collections
default=collections.defaultdict(list)
for i in group:
    for dict_s in i:
        for key,value in dict_s.items():
            default["list_user_id_{}".format(key)].append(value)


print(default)

output: 输出:

{'list_user_id_8': [67, 71, 68, 68, 85, -66], 'list_user_id_7': [79, 77, 86, 86, 59, -71], 'list_user_id_6': [81, 68, 71, 71, 67, -72]}

Second method: 第二种方法:

You can implement your own logic without importing any logic : 您可以实现自己的逻辑,而无需导入任何逻辑:

track={}
for i in group:
    for dict_s in i:
        for key,value in dict_s.items():
            if "list_user_id_{}".format(key) not in track:
                track["list_user_id_{}".format(key)]=[value]
            else:
                track["list_user_id_{}".format(key)].append(value)

print(track)

output: 输出:

{'list_user_id_8': [67, 71, 68, 68, 85, -66], 'list_user_id_7': [79, 77, 86, 86, 59, -71], 'list_user_id_6': [81, 68, 71, 71, 67, -72]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM