python 两个列表添加
问题描述
有一组列表,a,b,c,……,想要将这一组列表不断添加到同一个列表里面,比如:
`a = [’a’]b = [’b’]c = [’c’]addall = [[’a’], [’b’], [’c’]]`
只想到了用for循环来做这个,有什么比较pythonic的方法么?
问题解答
回答1:没必要太在意形式,简洁易于理解就行
a = [’a’]b = [’b’]c = [’c’]tt=[]tt.append(a)tt.append(b)tt.append(c)print tt#输出[[’a’], [’b’], [’c’]]回答2:
In [1]: a = [’a’, ’b’, ’c’] In [2]: b = [’d’, ’e’, ’f’] In [3]: import itertools In [4]: itertools.chain(a, b) Out[4]: <itertools.chain at 0x30fcd90> In [5]: list(itertools.chain(a, b)) Out[5]: [’a’, ’b’, ’c’, ’d’, ’e’, ’f’] 回答3:
python2,3
In [6]: a=[’a’]In [7]: b=[’b’]In [8]: a.extend(b)In [9]: aOut[9]: [’a’, ’b’]
python2,3,我觉得这个比较自然!
In [1]: a=[’a’]In [2]: b=[’b’]In [3]: a+bOut[3]: [’a’, ’b’]
python3
In [1]: a=[’a’]In [2]: b=[’b’]In [3]: [*a,*b]Out[3]: [’a’, ’b’]回答4:
d = [i for i in (a,b,c)]
相关文章:
1. docker安装后出现Cannot connect to the Docker daemon.2. docker镜像push报错3. thinkphp5.1学习时遇到session问题4. python - Django中如何使用异步5. transform - CSS3的3D变换多次变换如何保持坐标轴不动,或者有矩阵算法可以实现否6. debian - docker依赖的aufs-tools源码哪里可以找到啊?7. docker-machine添加一个已有的docker主机问题8. javascript - weex和node,js到底是怎样一个关系呢?9. python - pythoh3 下 ’<abc>’ 遇到这样的html转义符如何自动转义呢?10. python3.x - 如何将python3.4的程序转为python2.7
