我有一个Python代码,它没有读我想要它读的东西。 对于最差的车,福特应该是最差的车,但我一直得到雪佛兰ssr的结果有什么帮助吗?在
这是我正在处理的文件的格式:
1899 Horsey Horseless
1909 Ford Model T
1911 Overland OctoAuto
1913 Scripps-Booth Bi-Autogo
1920 Briggs and Stratton Flyer
1933 Fuller Dymaxion
1934 Chrysler/Desoto Airflow
1949 Crosley Hotshot
1956 Renault Dauphine
1957 King Midget Model III
这是我的密码:
^{pr2}$
除了代码的不正确的索引(这可能是您粘贴到问题中时的一个错误),它只有一个真正的问题:
这不会生成一个元组,只会将
^{pr2}$line[0]
分配给car_tuple
。要使其成为元组,需要包含逗号:但是,这不会改变您以后在尝试解包元组时遇到的问题,因此您应该只使用空字符串作为第二个元组项:
然后,您的代码将得到正确的结果:
也就是说,您可以通过使用更多的Python技巧来简化这一切。这是我的8行解决方案,加上注释,以便您了解情况:
相关问题 更多 >
编程相关推荐