[Python] 纯文本查看 复制代码 def group_data(data):
B = []
temp = []
count = 0
for i in range(len(data)):
if data[i][1] == 0:
temp.append(data[i][0])
count += 1
if count == 100:
B.append((temp[:], 0, len(temp)))
temp = []
count = 0
elif data[i][1] == 1:
if len(temp) > 0:
gap = 0
for j in range(i, len(data)):
if data[j][1] == 0:
break
gap += 1
if gap <= 2:
temp.append(data[i-gap][0])
temp.append(data[j][0])
B.append((temp[:], 1, gap+2))
temp = []
count = 0
i = j
elif data[i][1] == 2:
if len(temp) > 0:
B.append((temp[:], 0, len(temp)))
temp = []
count = 0
gap = 0
for j in range(i, len(data)):
if data[j][1] != 2:
break
temp.append(data[j][0])
gap += 1
B.append((temp[:], 2, gap))
temp = []
count = 0
i = j-1
if len(temp) > 0:
B.append((temp[:], 0, len(temp)))
return B
data = [(0,0),(1,0),(2,0),(3,1),(4,1),(5,0),(6,0),(7,2),(8,1),(9,0)]
B = group_data(data)
for group in B:
print(f"第{B.index(group)+1}组:{group[0]} 标记为{group[1]} 数量{group[2]}")
|