python中flat将嵌套列表中的元素按顺序排列在一个列表中

flat([[1,2,3],[4,5,6],[7,8,9]]) = [1,2,3,4,5,6,7,8,9]
def flat(nestedlist):
outcome = []
请问怎么做
return outcome

按照你的要求编写的Python程序如下

def flat(nestedlist):

outcome = [nestedlist[i][j] for i in range(len(nestedlist)) for j in range(len(nestedlist[i]))]

return outcome

print(flat([[1,2,3],[4,5,6],[7,8,9]]))

源代码(注意源代码的缩进)

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-06-19
2018-01-12693
相似回答