我有一个表单
中不同属性的词典列表attribute = [{user_id1:Value},{user_id2:Value};{User_id3:value3}]
这些是我的清单
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}]
我需要做的是为每个用户创建一个列表,例如:
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]
编辑: 为了解释我做了什么,我有一个调查django应用程序,我使用Chart.JS来呈现结果,必须采用以下形式: a = [number1,number2,number3]
现在这些数字是来自作为团队一部分的多个用户值的计算,所以我创建了一个方法,对于特定团队的每个成员,我提取一些值进行计算以给我一个分数然后我追加使用{id:score}
形式的member_id在列表中得分每个a,b,c,d都有一个计算方法来创建一个分数
这是方法之一的一个例子:
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:就像你可以看到我在编码领域的几个月,所以任何提示都欢迎进步;)
我该怎么做呢? 非常感谢你
答案 0 :(得分:0)
您可能希望将列表存储在新的词典中:
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]}
答案 1 :(得分:0)
首先将您的dicts列表转换为更好的数据结构,如字典:
lists = [a, b, c, d, e, f]
dictionaries = [dict(k.items()[0] for k in x) for x in lists]
现在获取所有可能的密钥:
keys = {y for y in x.keys() for x in dictionaries}
最后形成每个键的列表:
result = {k: [d[k] for d in dictionaries] for k in keys}
答案 2 :(得分:0)
您可以尝试两种方法:
数据是:
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]
第一种方法:
一个是使用默认字典:
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)
输出:
{'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]}
第二种方法:
您可以在不导入任何逻辑的情况下实现自己的逻辑:
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)
输出:
{'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]}