如果list里里面的元素是数字,‘1’也是可以的,那么我们可以使用np.ravel
a = [[1,2,3], [5, 2, 8], [7,8,9]]
list(np.ravel(a))
#[1, 2, 3, 5, 2, 8, 7, 8, 9]
对于不规则List
c=[[‘云阳站’], [‘双江’, ‘木古’], [‘滨双线’], [‘滨双线’, ‘云田线’], [‘云田线’, ‘田红线’]]
c=[['云阳站'], ['双江', '木古'], ['滨双线'], ['滨双线', '云田线'], ['云田线', '田红线']]
def flat(a):
l= []
for i in a:
if type(i) is list:
for j in i:
l.append(j)
else:
l.append(i)
return(l)
print(flat(c))