zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python最优效率的编程方式

Python效率编程 方式 最优
2023-09-11 14:14:27 时间
5. 字典合并

x = {'a': 1, 'b': 2}

y = {'b': 3, 'c': 4}

z = {**x, **y}
6. 反转字符串

name = "George"

name[::-1]
9. 遍历字典

m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

for key, value in m.items():

print('{0}: {1}'.format(key, value))
10. 同时遍历列表的索引和值

m = ['a', 'b', 'c', 'd']

for index, value in enumerate(m):

print('{0}: {1}'.format(index, value))
11. 初始化空容器

a_list = list()

a_dict = dict()

a_map = map()

a_set = set()
13. 列表中出现最多的元素

test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4]

print(max(set(test), key = test.count))