你写的
b[3]和b[4]之间的间隙大于3
但不是更大。它是更大的或相等的。
a = [0, 0, 2, 2, 2, 2]
b = [1, 3, 2, 3, 6, 7]
c = []
for s in set(a):
i = a.index(s)
count = a.count(s)
pom = []
for j in range(i, i + count):
if not pom:
pom.append(b[j])
elif abs(pom[-1] - b[j]) < 3:
pom.append(b[j])
if j + 1 == count:
pom.insert(0, s)
c.append(pom)
pom = []
else:
pom.insert(0, s)
c.append(pom)
pom = [b[j]]
print(c)