Python two lists added
習慣沉默
習慣沉默 2017-05-18 10:58:37
0
4
703

There is a set of lists, a, b, c,..., and I want to continuously add this set of lists to the same list, for example:

`a = ['a']
b = ['b']
c = ['c']
addall = [['a'], ['b'], ['c']]`

I only thought of using a for loop to do this. Is there any more pythonic method?

習慣沉默
習慣沉默

reply all(4)
巴扎黑

There is no need to pay too much attention to the form, just be concise and easy to understand

a = ['a']
b = ['b']
c = ['c']
tt=[]
tt.append(a)
tt.append(b)
tt.append(c)
print tt
#输出[['a'], ['b'], ['c']]
淡淡烟草味
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']      
                                            
刘奇

python2,3

In [6]: a=['a']

In [7]: b=['b']

In [8]: a.extend(b)

In [9]: a
Out[9]: ['a', 'b']

python2,3, I think this is more natural!

In [1]: a=['a']

In [2]: b=['b']

In [3]: a+b
Out[3]: ['a', 'b']

python3

In [1]: a=['a']

In [2]: b=['b']

In [3]: [*a,*b]
Out[3]: ['a', 'b']
Peter_Zhu

d = [i for i in (a,b,c)]

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template