Python中多个列表与字典的合并方法

Python中多个列表与字典的合并方法

1多列表的合并

1)a+=b

a=['','','']
b = ['','','']
a += b
print(a) >>>['','','','','','']

2) a.extend(b)

a=['','','']
b = ['','','']
a.extend(b)
print(a)
>>>['1','2','3','22','55','66']

3) a[0:0] = b切片的方法

a=['','','']
b = ['','','']
a[len(a):len(a)] = b
print(a) >>>['','','','','','']

2 多字典的合并

1) dict(a,**b)

A = {'a' :1,'b':2,'c':3}
B = {'h':55,'s':66,'d':88}
C = dict(A,**B)
print(C) >>>{'a' :1,'b':2,'c':3,'h':55,'s':66,'d':88}

2) dict(a.items()+b.items())

A = {'a' :1,'b':2,'c':3}
B = {'h':55,'s':66,'d':88}
C = dict(A.items()+B.items())print(C) >>>{'a' :1,'b':2,'c':3,'h':55,'s':66,'d':88}

3)c = {}  c.update(a)  c.update(b)

A = {'a' :1,'b':2,'c':3}
B = {'h':55,'s':66,'d':88}
C = {}
C.update(A)
C.update(B)
print(C) >>>{'a' :1,'b':2,'c':3,'h':55,'s':66,'d':88}
上一篇:Python中的split()函数的使用方法


下一篇:springboot2 webflux 响应式编程学习路径