取消列出一个复杂的 python 列表
Unlist a complex 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 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
试试这个代码
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']
我有一个列表:
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
试试这个代码
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']