如何将一下嵌套list,输出为一个list?
[[“a”], [“b”, “c”], [“d”], [“e”]]
sum(iterable[, start])
In [1]: list=[["a"], ["b", "c"], ["d"], ["e"]]
...: sum(list,[])
Out[1]: ['a', 'b', 'c', 'd', 'e']
sum函数的作用机理是
[“a”]+[“b”, “c”]+[“d”]+[“e”]+[]
注意第二个参数如果不填的话,会默认为0,如果为0这个就会报错因为作用的机理是
[“a”]+[“b”, “c”]+[“d”]+[“e”]+0,列表和0相加会报错
functools.reduce
In [1]: import functools
...: list=[["a"], ["b", "c"], ["d"], ["e"]]
...: functools.reduce(lambda x, y:x+y,list)
Out[1]: ['a', 'b', 'c', 'd', 'e']
itertools.chain()
’ ‘.join(sequence)
最后使用join函数可以将list转换为字符串
参考文献
https://stackoverflow.com/questions/11860476/how-to-unnest-a-nested-list