需求描述
给你一个列表如下
代码语言:javascript复制[1,2,3,1,2,4,5,6,7,1,4,5,7,1,2,3]
请你算出任意两个数字一起出现的次数,比如(1,2)一共出现了3次,(2,3)一共出现了2次,(3,1)出现了1次。
代码语言:javascript复制s = [1,2,3,1,2,4,5,6,7,1,4,5,7,1,2,3]
def get_stats(ids):
count = {}
for pair in zip(ids, ids[1:]):
count[pair] = count.get(pair, 0) 1
return count
stats = get_stats(s)
print(stats)
输出
代码语言:javascript复制{(1, 2): 3, (2, 3): 2, (3, 1): 1, (2, 4): 1, (4, 5): 2, (5, 6): 1, (6, 7): 1, (7, 1): 2, (1, 4): 1, (5, 7): 1}