怎么对python中列表类型进行分组
展开全部
嗯,来个简短版的(两行代码):
In [1]: from itertools import groupbyIn [2]: data = ["1、abc","2、afd","3、ad","4、ajh","1、fc","2、acxcf","3、fcacxi","1、acxcf", "2、rrr", "1、adsfdd","2、acxcfxxc"]In [3]: f = lambda s, n=[]: s[0]=='1' and n.append(0) or len(n)In [4]: [list(g) for _, g in groupby(data, f)]Out[4]: [['1、abc', '2、afd', '3、ad', '4、ajh'],
['1、fc', '2、acxcf', '3、fcacxi'],
['1、acxcf', '2、rrr'],
['1、adsfdd', '2、acxcfxxc']]
In [1]: from itertools import groupbyIn [2]: data = ["1、abc","2、afd","3、ad","4、ajh","1、fc","2、acxcf","3、fcacxi","1、acxcf", "2、rrr", "1、adsfdd","2、acxcfxxc"]In [3]: f = lambda s, n=[]: s[0]=='1' and n.append(0) or len(n)In [4]: [list(g) for _, g in groupby(data, f)]Out[4]: [['1、abc', '2、afd', '3、ad', '4、ajh'],
['1、fc', '2、acxcf', '3、fcacxi'],
['1、acxcf', '2、rrr'],
['1、adsfdd', '2、acxcfxxc']]
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询