以下是一种解决方法,其中包含示例代码:
示例代码:
def group_sort(lst):
result = []
group = [lst[0]]
for i in range(1, len(lst)):
if lst[i] == lst[i-1]:
group.append(lst[i])
else:
result.append(group)
group = [lst[i]]
result.append(group)
result.sort(key=len, reverse=True)
return result
# 示例输入
input_list = [1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4]
# 调用函数并打印输出
print(group_sort(input_list))
输出结果:
[[4, 4, 4, 4], [2, 2, 2], [1, 1], [3, 3]]
在示例输入中,相同的数字被分组为 [1, 1], [2, 2, 2], [3, 3] 和 [4, 4, 4, 4]。根据组的长度进行排序后,结果为 [[4, 4, 4, 4], [2, 2, 2], [1, 1], [3, 3]]。
上一篇:按连续条件筛选行 - 按组分组
下一篇:按连续行分组