取消列出复杂的python列表
我的名单如下:取消列出复杂的python列表,python,list,Python,List,我的名单如下: mylist = [['469536.18999', '6334694.44001', '-9999.0'], '-9999.0', '-9999.0', '-9999.0', '-9999.0'] 我希望以下列为: ['469536.18999', '6334694.44001', '-9999.0', '-9999.0', '-9999.0', '-9999.0', '-9999.0'] 我使用了不同的方法,但总是得到错误的结果: from itertools impor
mylist = [['469536.18999', '6334694.44001', '-9999.0'], '-9999.0', '-9999.0', '-9999.0', '-9999.0']
我希望以下列为:
['469536.18999', '6334694.44001', '-9999.0', '-9999.0', '-9999.0', '-9999.0', '-9999.0']
我使用了不同的方法,但总是得到错误的结果:
from itertools import chain
print list(chain.from_iterable(mylist))
['469536.18999', '6334694.44001', '-9999.0', '-', '9', '9', '9', '9', '.', '0', '-', '9', '9', '9', '9', '.', '0', '-', '9', '9', '9', '9', '.', '0', '-', '9', '9', '9', '9', '.', '0']
sum(mylist, [])
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
来自itertools导入链的
打印列表(链从列表(mylist))
['469536.18999', '6334694.44001', '-9999.0', '-', '9', '9', '9', '9', '.', '0', '-', '9', '9', '9', '9', '.', '0', '-', '9', '9', '9', '9', '.', '0', '-', '9', '9', '9', '9', '.', '0']
总和(mylist,[])
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:只能将列表(而不是“str”)连接到列表
试试这段代码
c=[['469536.18999', '6334694.44001', '-9999.0'], '-9999.0', '-9999.0', '-9999.0', '-9999.0']
x=[]
for i in range(len(c)):
if 'list' in str(type(c[i])):
for j in range(len(c[i])):
x.append(c[i][j])
else :
x.append(c[i])
print x
输出:
['469536.18999'、'6334694.44001'、'-9999.0'、'-9999.0'、'-9999.0'、'-9999.0'、'-9999.0'、'-9999.0']