4 回答
TA贡献1829条经验 获得超13个赞
你不能像这样对 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)
TA贡献1784条经验 获得超2个赞
您可以在列表理解中使用过滤器来忽略除 2 项以外的列表:
dict_of_parsed_rows = dict([l for l in list_of_parsed_rows if len(l) == 2])
TA贡献1806条经验 获得超5个赞
您正在遍历嵌套列表并且内部列表的长度不同。因此,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_rows
Out[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}
TA贡献1873条经验 获得超9个赞
在示例中,没有定义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}
添加回答
举报