1.list数据分割为多个小列表 (java lists.partition)
2. 分组
import itertools def partition(mylist, size): """ :param mylist: 需要分割的列表 :param size: 每几个分割一起 :return: [[],[]] """ return [mylist[i:i + size] for i in range(0, len(mylist), size)] def groupby(mylist,key): """ :param mylist: eg: [{"user":"sea","age":"23"},{"user":"sea1","age":"22"}] :param key: eg: user :return: """ result = {} # lstg = groupby(mylist,key=lambda x:x['no']) lstg = itertools.groupby(mylist, key=lambda x: x[key]) for (key, group) in lstg: key_group = [] for g in group: key_group.append(g) result[key] = key_group return result if __name__ == '__main__': # 分割 l = [i for i in range(15)] partition1 = partition(l, 3) print(partition1)
标签:__,group,python,list,param,util,key,mylist,size From: https://www.cnblogs.com/lshan/p/16910892.html