|
发表于 2021-12-23 16:20:21
From FishC Mobile
|
显示全部楼层
本帖最后由 傻眼貓咪 于 2021-12-23 16:29 编辑
因为我现在用手机笔记本写的,代码有点长,懒得优化:- data = """location1..100000
- location1..100001
- location1..100002
- location1..100003
- location1..100004
- location1..100005
- location1..100008
- location1..100009
- location1..100010
- location2..100000
- location2..100001
- location2..100002
- location2..100003
- location2..100004
- location2..100005
- location2..100006
- location3..100018
- location3..100019
- location3..100020
- location3..100021
- location3..100022
- location3..100026
- location3..100027
- location3..100028
- location3..100029
- location3..100030
- location3..100031
- location3..100032
- location3..100033"""
- arr = [(list(item.split(".."))[0], int(list(item.split(".."))[1])) for item in data.split("\n")]
- sorted(arr, key = lambda x: x[1])
- x = arr[0][0]
- y = arr[0][1]
- res = []
- temp = []
- for n, each in enumerate(arr):
- a, b = each
- if(a != x or b != y):
- res.append(temp)
- temp = [(a, b)]
- x = a
- y = b+1
- elif n == len(arr)-1:
- temp.append((a, b))
- res.append(temp)
- else:
- temp.append((a, b))
- y += 1
- del arr
- for each in res:
- print(each[0][0], each[0][1], each[-1][1], each[-1][1]-each[0][1]+1)
复制代码 结果:- location1 100000 100005 6
- location1 100008 100010 3
- location2 100000 100006 7
- location3 100018 100022 5
- location3 100026 100033 8
复制代码 |
|