【发布时间】:2015-12-14 15:25:55
【问题描述】:
我需要帮助构建一个字典,它有一个键,然后值是一个列表的列表,其中有一个列表,其中键值与列表的第三项匹配,然后将其放在该键下 (我会尝试举例说明,因为它很难用词)
#This is the score users achieve and needs to be the keys of the dictionary)
keyScores = [5,4,3,2,1]
# The data represents at [0] =user_id , [1] = variables for matching,[2] = scores
# (if its score == to a dictionary key then place it there as a value )
fetchData = [
[141, [30, 26, 7, 25, 35, 20, 7], 5],
[161, [36, 13, 29], 5],
[166, [15, 11, 25, 7, 34, 28, 17, 28],3]
]
#I need to build a dictionary like this:
{5: [[141, [30, 26, 7, 25, 35, 20, 7],[161, [36, 13, 29]],
3:[[166, [15, 11, 25, 7, 34, 28, 17, 28]
}
我正在考虑使用
中表示的 defaultdictPython creating a dictionary of lists
我无法正确解包。
任何帮助都会很棒。
谢谢。
【问题讨论】:
标签: dictionary python-3.3 defaultdict