lis = ["任务1", "任务2", "作者1", "作者2"]
# 根据lis内元素的前两个字符, 将lis分割为[["任务1", "任务2"], ["作者1", "作者2"]]
# 方法1
from itertools import groupby
result = [list(group) for key, group in groupby(sorted(lis), key=lambda x: x[:2])]
# 方法2
result = {}
for item in lis:
key = item[:2]
if key not in result:
result[key] = []
result[key].append(item)
result = list(result.values())
标签:item,任务,作者,result,key,lis
From: https://blog.51cto.com/u_16055028/9197861