1.两个字典:a={'a':1,'b':2,'c':3} b= {'aa':11,'bb':22,'cc':33}
合并1:dict(a,**b) 操作如下:
>>a = {'a':1, 'b':2, 'c':3}
>>b = {'aa':11, 'b':22, 'c':33}
>>dict(a,**b)
{'a':1,'b':2,'c':3, 'aa':11,'bb':22,'cc':33}
合并2:c = {} c.update(a) c.update(b) 输出c 如下:
>>c = {}
>>c.update(a)
>>c.update(b)
>>print(c)
{'a':1,'b':2,'c':3, 'aa':11,'bb':22,'cc':33}
2.两个list合并:a=[1,2,3] 、 b=['a','b','c']
合并1:a+b如下:
>>a=[1,2,3]
>>b=['a','b','c']
>>a+b
[1,2,3,'a','b','c']
合并2:a+=b如下:
>>a=[1,2,3]
>>b=['a','b','c']
>>a+=b
>>print(a)
[1,2,3,'a','b','c']
合并3:a.extend(b)如下:
>>a=[1,2,3]
>>b=['a','b','c']
>>a.extend(b)
>>print(a)
[1,2,3,'a','b','c']
合并4:a.append(b)如下:
>>a=[1,2,3]
>>b=['a','b','c']
>>a.append(b)
>>print(a)
[1,2,3,['a','b','c']]
合并5:a[0:0] = b如下:
>>a=[1,2,3]
>>b=['a','b','c']
>>a[0:0] = b
>>print(a)
['a','b','c',1,2,3]