拼贴列表列表的Python错误

时间:2016-10-18 06:58:03

标签: python list flatten

您好我试图压扁以下列表列表,但我总是收到以下错误:

' INT'对象不可迭代

我也试过来自itertools的链但仍然没有工作。我想解决方案很简单,但我真的看不到它!有人可以帮忙吗?

由于

 from itertools import chain

import operator

lista = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]

listone = lista[0][0],[-x[0] for x in lista[:2]]

#sumlistone = chain.from_iterable(listone)

sumlistone = [x for sublist in listone for x in sublist]

print listone

print sumlistone

1 个答案:

答案 0 :(得分:0)

这是你需要的吗?

lista = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
listb = []

for sub in lista:
    listb.extend(sub)    # Modification suggested by @Dinesh Pundkar 

print(listb)
print(sum(listb))