字典合并;函数返回值同时用于判断与输出

发布时间 2023-05-02 10:24:40作者: Aneverforget
 1 #普通字典update,与Counter update不同
 2 d1={"1":2,"2":2}
 3 d2={"1":1,"2":2}
 4 print(d2)
 5 #{'1': 1, '2': 2}
 6 d2.update(d1)
 7 print(d2)
 8 #{'1': 2, '2': 2}
 9 from collections import Counter
10 d1=Counter(d1)
11 d2=Counter(d2)
12 new=dict(d1+d2)
13 print(new)
14 #{'1': 4, '2': 4}
15 d2.update(d1)
16 print(d2)
17 #Counter({'1': 4, '2': 4})
18 print(dict(d2))
19 #{'1': 4, '2': 4}

 

1 def tem():
2     return 4
3 #函数返回值不像类生成实例,可以用tem()形式得到相同的返回值。
4 if tem():  #tem()函数返回值同时用于判断与输出
5     print(tem())
6     #4
7     print(tem())
8     #4