List 我只想在python中更改列表中某些元素的数据类型
我有一个列表,里面有这样的东西List 我只想在python中更改列表中某些元素的数据类型,list,type-conversion,List,Type Conversion,我有一个列表,里面有这样的东西 [['11.0' 'Switchgear and Protection' '' '' '' '25569.0' '26299.0'] ['12.0' 'Synchronising' '' '' '' '25569.0' '26299.0'] ['13.0' 'Transformers and Protection' '' '' '' '25569.0' '26299.0'] ['14.0' 'Turbine' '' '' 'Name' '25569.0'
[['11.0' 'Switchgear and Protection' '' '' '' '25569.0' '26299.0']
['12.0' 'Synchronising' '' '' '' '25569.0' '26299.0']
['13.0' 'Transformers and Protection' '' '' '' '25569.0' '26299.0']
['14.0' 'Turbine' '' '' 'Name' '25569.0' '26299.0']
['15.0' 'Cooling Water' '' '' '' '25569.0' '26299.0']
['16.0' 'Water Cycle' '' '' '' '25569.0' '26299.0']]
我想将第一个和最后两个冒号更改为int类型,但未成功。(学习一门新语言并不是人们所说的全部…)
谢谢
保罗这里有一种可能的方法:
listoflists=[ ['11.0', 'Switchgear and Protection', '', '', '', '25569.0', '26299.0'],
['12.0', 'Synchronising', '', '', '', '25569.0', '26299.0'],
['13.0', 'Transformers and Protection', '', '', '', '25569.0', '26299.0'],
['14.0', 'Turbine', '', '', 'Name', '25569.0', '26299.0'],
['15.0', 'Cooling Water', '', '', '', '25569.0', '26299.0'],
['16.0', 'Water Cycle', '', '', '', '25569.0', '26299.0'] ]
for i in xrange(len(listoflists)):
for j in xrange(len(listoflists[i])):
try:
listoflists[i][j] = int(float(listoflists[i][j]))
except:
pass
我不确定是否有更简单的方法。欢迎来到Stack Overflow,请[参观](),确保您阅读并更新了您的问题,了解更多信息。