我有:
master_list = [['001', '15\n', '963789', '40\n', '741239', '80\n', '985697', '80\n', '854698', '35\n', '965874', '10\n'],
['002', '25\n', '326574', '65\n', '944223', '40\n', '312689', '45\n', '225869', '80\n', '789654', '35\n'],
['003', '10\n', '857963', '50\n', '253698', '40\n', '965478', '50\n', '186458', '40\n', '351296', '40\n'],
['004', '20\n', '675964', '40\n', '612985', '40\n', '653674', '35\n', '957296', '50\n', '852169', '40\n'],
['005', '13', '246912', '40\n', '371956', '40\n', '819736', '40\n', '915745', '50\n', '197548', '40']]
我试图解压缩为:
for a,b,c,d,e, in master_list:
print(a,b,c,d,e)
获得例外:
ValueError: too many values to unpack (expected 5)
为什么会这样。我没有看错任何错误
答案 0 :(得分:2)
错误是正确的,您在嵌套列表中有12个项目,并且您只提供了5个要解压缩的变量。如果您只是打印它们,您可以这样做:
for l in master_list:
print(*l)
答案 1 :(得分:0)
在python3中你会用这种方式解决任意长度的问题:
for a,b,c,d,e,*_ in master_list:
print(a,b,c,d,e)