从 2D 列表到 dict 时要解压的值太多(预期为 2)?(Python)

我有一个包含 2 个项目列表的列表,如下所示:


list_of_parsed_rows =

[['ADP:', 'Round: 1, Pick: 2'],

 ['Team:', 'Carolina Panthers'],

 ['Ht / Wt:', '5\'11" / 202 lb.'],

 ['College:', 'Stanford'],

 ['Age:', '24'],

 ['Born:', 'June 7, 1996'],

 ['Drafted:', '2017 /\n\nRd. 1 (8)'],

 ['Draft Team:', 'CAR'],

 ['Saquon Barkley', 'RB', 'NYG', '1.02'],

 ['Ezekiel Elliott', 'RB', 'DAL', '1.04'],

 ['Derrick Henry', 'RB', 'TEN', '1.04'],

 ['Dalvin Cook', 'RB', 'MIN', '1.05'],

 ['Alvin Kamara', 'RB', 'NO', '1.06'],

 ['Michael Thomas', 'WR', 'NO', '1.08'],

 ['Josh Jacobs', 'RB', 'LV', '1.08'],

 ['Nick Chubb', 'RB', 'CLE', '1.09'],

 ['Aaron Jones', 'RB', 'GB', '1.10'],

 ['Tyreek Hill', 'WR', 'KC', '1.11'],

 ['Joe Mixon', 'RB', 'CIN', '1.11'],

 ['Pat Mahomes', 'QB', 'KC', '2.01'],

 ['Davante Adams', 'WR', 'GB', '2.01'],

 ['DeAndre Hopkins', 'WR', 'ARI', '2.02'],

 ['Miles Sanders', 'RB', 'PHI', '2.03']]

我正试图用以下内容将它变成一个字典


dict_of_parsed_rows = {key: value for key, value in list_of_parsed_rows}

但我收到以下错误消息:


ValueError: too many values to unpack (expected 2)

知道发生了什么吗?奇怪的是,这是一本书中问题的确切解决方案,我很惊讶它是不正确的。


慕码人8056858
浏览 115回答 4
4回答

烙印99

你不能像这样对 dict 使用列表理解。Python 不理解您的意思list[0]是键和list[1]值。但是你可以用一个循环来完成。list_of_parsed_rows = [['ADP:', 'Round: 1, Pick: 2'], ['Team:', 'Carolina Panthers'], ['Ht / Wt:', '5\'11" / 202 lb.'], ['College:', 'Stanford'], ['Age:', '24'], ['Born:', 'June 7, 1996'], ['Drafted:', '2017 /\n\nRd. 1 (8)'], ['Draft Team:', 'CAR'], ['Saquon Barkley', 'RB', 'NYG', '1.02'], ['Ezekiel Elliott', 'RB', 'DAL', '1.04'], ['Derrick Henry', 'RB', 'TEN', '1.04'], ['Dalvin Cook', 'RB', 'MIN', '1.05'], ['Alvin Kamara', 'RB', 'NO', '1.06'], ['Michael Thomas', 'WR', 'NO', '1.08'], ['Josh Jacobs', 'RB', 'LV', '1.08'], ['Nick Chubb', 'RB', 'CLE', '1.09'], ['Aaron Jones', 'RB', 'GB', '1.10'], ['Tyreek Hill', 'WR', 'KC', '1.11'], ['Joe Mixon', 'RB', 'CIN', '1.11'], ['Pat Mahomes', 'QB', 'KC', '2.01'], ['Davante Adams', 'WR', 'GB', '2.01'], ['DeAndre Hopkins', 'WR', 'ARI', '2.02'], ['Miles Sanders', 'RB', 'PHI', '2.03']]dict_of_parsed_rows = dict()for elem in list_of_parsed_rows:    dict_of_parsed_rows[elem[0]] = elem[1:]print(dict_of_parsed_rows)

一只斗牛犬

您可以在列表理解中使用过滤器来忽略除 2 项以外的列表:dict_of_parsed_rows = dict([l for l in list_of_parsed_rows if len(l) == 2])

忽然笑

您正在遍历嵌套列表并且内部列表的长度不同。因此,python 无法解压很多值。假设希望第一个元素为key,其余元素为value,则需要将剩余元素拼接成一个字符串In [2]: list_of_parsed_rows =[['ADP:', 'Round: 1, Pick: 2'],   ...:  ['Team:', 'Carolina Panthers'],   ...:  ['Ht / Wt:', '5\'11" / 202 lb.'],   ...:  ['College:', 'Stanford'],   ...:  ['Age:', '24'],   ...:  ['Born:', 'June 7, 1996'],   ...:  ['Drafted:', '2017 /\n\nRd. 1 (8)'],   ...:  ['Draft Team:', 'CAR'],   ...:  ['Saquon Barkley', 'RB', 'NYG', '1.02'],   ...:  ['Ezekiel Elliott', 'RB', 'DAL', '1.04'],   ...:  ['Derrick Henry', 'RB', 'TEN', '1.04'],   ...:  ['Dalvin Cook', 'RB', 'MIN', '1.05'],   ...:  ['Alvin Kamara', 'RB', 'NO', '1.06'],   ...:  ['Michael Thomas', 'WR', 'NO', '1.08'],   ...:  ['Josh Jacobs', 'RB', 'LV', '1.08'],   ...:  ['Nick Chubb', 'RB', 'CLE', '1.09'],   ...:  ['Aaron Jones', 'RB', 'GB', '1.10'],   ...:  ['Tyreek Hill', 'WR', 'KC', '1.11'],   ...:  ['Joe Mixon', 'RB', 'CIN', '1.11'],   ...:  ['Pat Mahomes', 'QB', 'KC', '2.01'],   ...:  ['Davante Adams', 'WR', 'GB', '2.01'],   ...:  ['DeAndre Hopkins', 'WR', 'ARI', '2.02'],   ...:  ['Miles Sanders', 'RB', 'PHI', '2.03']]In [3]: dict_of_parsed_rows = {innerlist[0]:",".join(innerlist[1:]) for innerlist in list_of_parsed_rows}In [4]: dict_of_parsed_rowsOut[4]:{'ADP:': 'Round: 1, Pick: 2', 'Team:': 'Carolina Panthers', 'Ht / Wt:': '5\'11" / 202 lb.', 'College:': 'Stanford', 'Age:': '24', 'Born:': 'June 7, 1996', 'Drafted:': '2017 /\n\nRd. 1 (8)', 'Draft Team:': 'CAR', 'Saquon Barkley': 'RB,NYG,1.02', 'Ezekiel Elliott': 'RB,DAL,1.04', 'Derrick Henry': 'RB,TEN,1.04', 'Dalvin Cook': 'RB,MIN,1.05', 'Alvin Kamara': 'RB,NO,1.06', 'Michael Thomas': 'WR,NO,1.08', 'Josh Jacobs': 'RB,LV,1.08', 'Nick Chubb': 'RB,CLE,1.09', 'Aaron Jones': 'RB,GB,1.10', 'Tyreek Hill': 'WR,KC,1.11', 'Joe Mixon': 'RB,CIN,1.11', 'Pat Mahomes': 'QB,KC,2.01', 'Davante Adams': 'WR,GB,2.01', 'DeAndre Hopkins': 'WR,ARI,2.02', 'Miles Sanders': 'RB,PHI,2.03'}如果您希望第二个元素是值dict_of_parsed_rows = {key: value for key,value, *k in list_of_parsed_rows}

眼眸繁星

在示例中,没有定义value当有超过 2 个项目时会是什么,例如:['Saquon Barkley', 'RB', 'NYG', '1.02'],如果您想保留第一个元素(例如'Saquon Barkley')作为键并将其余值组合到一个list(例如['RB', 'NYG', '1.02'])中并用作您的value,一个简单的解决方法是使用解包运算符 ( *):dict_of_parsed_rows = {key: value for key, *value in list_of_parsed_rows}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python