简体   繁体   English

如何创建以列表为值的字典?

[英]How do I create a dictionary with lists as values?

How can I get this output:我怎样才能得到这个 output:

{'Test 1': ['100', '88', '45', '59', '73', '89'], 'Test 2': ['90', '99', '56', '61', '79', '97'], 'Test 3': ['80', '111', '67', '67', '83', '101']}

with this data有了这个数据

grades =  [
  ['Students', 'Test 1', 'Test 2', 'Test 3'],
  ['Tomas', '100', '90', '80'],
  ['Marcos', '88', '99', '111'],
  ['Flavia', '45', '56', '67'],
  ['Ramon', '59', '61', '67'],
  ['Ursula', '73', '79', '83'],
  ['Federico', '89', '97', '101']
] 

The first element (row) of grades contains all the keys you want to use. grades的第一个元素(行)包含您要使用的所有键。 Every other row contains the values of the keys in every column.每隔一行包含每一列中键的值。 You can use zip with unpacking like so: zip(grades[0], *grades[1:]) to obtain an iterator that gives a tuple containing the key as the first element, and the corresponding column from every other row in the remaining elements.您可以使用zip进行解包,如下所示: zip(grades[0], *grades[1:])获得一个迭代器,该迭代器给出一个包含键作为第一个元素的元组,以及剩余的每隔一行的对应列元素。 Then, you can unpack this using k, *v in zip(...) to get the key in k , and a list of the values in the column in v .然后,您可以使用k, *v in zip(...)以获取k中的键,以及v中列中的值列表。

Then, you can use a dictionary comprehension to create a dictionary out of the result of the zip operation.然后,您可以使用字典推导根据zip操作的结果创建字典。 k:v is keys and values for the dictionary, for k in grades[0] so each iteration will give k one element from grades[0] , then *v is a full vertical column. k:v是字典的键和值,对于k in grades[0]所以每次迭代都会给 k 一个来自grades[0]的元素,然后*v是一个完整的垂直列。

{k: v for k, *v in zip(grades[0], *grades[1:])}

# Result:
{'Students': ['Tomas', 'Marcos', 'Flavia', 'Ramon', 'Ursula', 'Federico'],
 'Test 1': ['100', '88', '45', '59', '73', '89'],
 'Test 2': ['90', '99', '56', '61', '79', '97'],
 'Test 3': ['80', '111', '67', '67', '83', '101']}

dictt ={}
for n,i in enumerate(grades[0][1:],start=1):
  dictt[i] = [x[n] for x in grades[1:]]

print(dictt)

>>> {'Test 1': ['100', '88', '45', '59', '73', '89'], 'Test 2': ['90', '99', '56', '61', '79', '97'], 'Test 3': ['80', '111', '67', '67', '83', '101']}

start = 1 omits from adding the labels to the dictionary values start = 1省略将标签添加到字典值

暂无
暂无

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

相关问题 如何将列表作为值的字典转换为整数作为值的字典? - How do I turn a dictionary with lists as values into a dictionary with integers as values? 如何用列表中的键和值分隔空列表创建字典? - How do I create a dictionary with keys from a list and values separate empty lists? 如何在列表的字典中搜索值 - How do I search Values in a dictionary which are lists 如何访问字典中包含的列表的第一个值? - How do I access the first values of lists contained in a dictionary? 如何在 Django 模板中使用列表迭代字典作为值? - How do I iterate dictionary with lists as values in Django template? 如何从列表列表中创建字典键? - How do I create dictionary keys from list of lists? 在 Python 中,给定一个包含值列表的字典,如何根据该列表中的项目数量对字典进行排序? - In Python, given a dictionary with lists in the values, how do I sort the dictionary based on the amount of items in that list? 如何将字符串字典转换为某些值为列表的字典? - How do I convert a string dictionary to dictionary where some values are lists? 如何创建字典外的列表,其名称与字典键相同,并且大小与该键的值相同? - How do I create lists out of dictionary, having same name as dictionary keys and same size as value of that key? 我如何对它们是列表的字典值进行排序或排序? - how I sort or sorted dictionary values that they are lists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM