简体   繁体   English

使用嵌套列表创建字典,其中第一个列表是列,每个列表是一行?

[英]Using a nested list to create dictionary where first list is column and each list a row?

I have a nested loop that has the first list as column names and each subsequent list is a row.我有一个嵌套循环,其中第一个列表作为列名,每个后续列表都是一行。 For example, I have a nested list that gives a person's age and 1 or 0 indicating whether or not they like the color:例如,我有一个嵌套列表,它给出了一个人的年龄和 1 或 0,表示他们是否喜欢这种颜色:

data = [['age', 'blue', 'green', 'red', 'orange'], 
         [12, 1, 0, 0, 1],
         [10, 0, 1, 0, 1],
         [9, 0, 0, 0, 0],
         [8, 1, 0, 0, 0],
         [13, 1, 1, 1, 0]

I want an output that looks like:我想要一个看起来像的 output:

 new_data ={(12, 'blue'): 1,
            (12, 'green'): 0,
            (12, 'red'): 0,
            (12, 'orange'): 1,
            (9, 'blue'): 0,
            (9, 'green'): 1,
            (9, 'red'): 0,
            (9, 'orange'): 1 .....}

etc but with all data.等,但所有数据。

I've gotten as far as:我已经做到了:

ages = data[0][1:len(data[0])]

i = 1
for x in ages:
    print({t[0] + x: t[i] for t in data[1:len(data)]})
    i = i + 1

and when I try to combine t[0] and x as a tuple it ends up only giving me:当我尝试将 t[0] 和 x 组合为一个元组时,它最终只给了我:

{'12blue': 1, '10blue': 0, '9blue': 0, '8blue': 1, '13blue': 1}
{'12green': 0, '10green': 1, '9green': 0, '8green': 0, '13green': 1} 

etc.等等

You could do the following:您可以执行以下操作:

import pprint

data = [['age', 'blue', 'green', 'red', 'orange'],
        [12, 1, 0, 0, 1],
        [10, 0, 1, 0, 1],
        [9, 0, 0, 0, 0],
        [8, 1, 0, 0, 0],
        [13, 1, 1, 1, 0]]

colors, *rows = data # split in colors and rows

d = {}
for row in rows: 
    idi, *marks = row # split in id and marks
    for color, mark in zip(colors[1:], marks): # iterate in parallel over marks and colors
            d[(idi, color)] = mark


pprint.pprint(d)

Output Output

{(8, 'blue'): 1,
 (8, 'green'): 0,
 (8, 'orange'): 0,
 (8, 'red'): 0,
 (9, 'blue'): 0,
 (9, 'green'): 0,
 (9, 'orange'): 0,
 (9, 'red'): 0,
 (10, 'blue'): 0,
 (10, 'green'): 1,
 (10, 'orange'): 1,
 (10, 'red'): 0,
 (12, 'blue'): 1,
 (12, 'green'): 0,
 (12, 'orange'): 1,
 (12, 'red'): 0,
 (13, 'blue'): 1,
 (13, 'green'): 1,
 (13, 'orange'): 0,
 (13, 'red'): 1}

暂无
暂无

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

相关问题 创建元组列表的列表,其中每个元组是第一个出现的字母及其列表中的行和列 - Create a list of lists of tuples where each tuple is the first occurrence of a letter along with its row and column in the list of lists 使用列表创建嵌套字典 - create nested dictionary using list 我可以使用 groupby 在 Pandas 数据框中创建每行都是运行列表的列吗? - Can I create column where each row is a running list in a Pandas data frame using groupby? 访问列表Python中每一行的第一列 - Access the first column in each row in a list Python Pandas,字典列表,其中值是一个列表。 将字典键转换为列名。 将值列表中的每个元素转换为一行 - Pandas, list of dictionaries where values are a list. Convert dictionary keys into column names. Convert each element in value list to a row 如何使用每个列表的第一个值作为键从列表列表中创建字典 - How can I create a dictionary from a list of lists using the first value of each list as the key 带有嵌套字典的列表中的数据帧,其中第一个字典的键是列和键,第二个字典的值是行和值 - DataFrame from list with nested dicts where key of first dict is column and key, value of second dict is row and value 创建 csv 文件,其中每一行都是一个列表中的一些嵌套列表 - create csv file where each row is some nested lists from one list 从嵌套字典创建列表 - Create List from Nested Dictionary 如何创建列表的嵌套字典 - how to create a nested dictionary of list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM