from itertools import groupbylst = [1, 2, 3, 5, 6, 7, 8, 11, 12, 13, 19] # 连续数字fun = lambda x: x[1]-x[0]for k, g in groupby(enumerate(lst), fun): l1 = [j for i, j in g] # 连续数字的列表 if len(l1) > 1: scop = str(min(l1)) + '-' + str(max(l1)) # 将连续数字范围用"-"连接 else: scop = l1[0] print("连续数字范围:{}".format(scop))
# 冒泡排序(从小到大)lst = [4, 2, 1, 5, 6, 7, 8, 11, 12, 13, 19]for i in range(len(lst)): j = i+1 for j in range(len(lst)): if lst[i] < lst[j]: x = lst[i] lst[i] = lst[j] lst[j] = xprint("排序后列表:{}".format(lst))