1 在两个变量之间交换值
在其他语言中,要在两个变量间交换值而不是用第三个变量,我们要么使用算术运算符,要么使用位异或(Bitwise XOR)。在 Python 中,它就简单多了,如下所示。...strings = ['50', 'python', 'snippets']
print(','.join(strings)) # 50,python,snippets
9 查找列表的第一个元素...def head(list):
return list[0]
print(head([1, 2, 3, 4, 5])) # 1
10 查找存在于两个列表中任一列表存在的元素
此函数返回两个列表中任一列表中的每个元素...return list(set(numbers))
unique_elements([1, 2, 3, 2, 4]) # [1, 2, 3, 4]
12 求一组数字的平均值
此函数返回列表中两个或多个数字的平均值...list = [1, 2, 3, 2, 4, 3, 2, 3]
count = Counter(list)
print(count) # {2: 3, 3: 3, 1: 1, 4: 1}
15 查找列表中最常用的元素