取消列出复杂的python列表

时间:2015-01-07 04:32:56

标签: 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

1 个答案:

答案 0 :(得分:1)

试试此代码

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',' - 999.0',' - 999.0',' - 9999.0',' - 999.0',' - 999.0']