在python中加入列表列表 [英] join list of lists in python

查看:48
本文介绍了在python中加入列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中将列表列表连接成单个列表(或迭代器)的简短语法是什么?

Is the a short syntax for joining a list of lists into a single list( or iterator) in python?

例如,我有一个如下列表,我想遍历 a、b 和 c.

For example I have a list as follows and I want to iterate over a,b and c.

x = [["a","b"], ["c"]]

我能想到的最好的如下.

The best I can come up with is as follows.

result = []
[ result.extend(el) for el in x] 

for el in result:
  print el

推荐答案

import itertools
a = [['a','b'], ['c']]
print(list(itertools.chain.from_iterable(a)))

这篇关于在python中加入列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆