有一組列表,a,b,c,…,想要將這一組列表不斷加到同一個列表裡面,例如:
`a = ['a']
b = ['b']
c = ['c']
addall = [['a'], ['b'], ['c']]`
只想到了用for迴圈來做這個,有什麼比較pythonic的方法麼?
巴扎黑2017-05-18 11:00:37
沒必要太在意形式,簡潔易懂就行
a = ['a']
b = ['b']
c = ['c']
tt=[]
tt.append(a)
tt.append(b)
tt.append(c)
print tt
#输出[['a'], ['b'], ['c']]
怪我咯2017-05-18 11:00:37
python2,3
In [6]: a=['a']
In [7]: b=['b']
In [8]: a.extend(b)
In [9]: a
Out[9]: ['a', 'b']
python2,3,我覺得這樣比較自然!
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']