简体   繁体   English

根据子列表的第一个元素拆分子列表列表

[英]split a list of sublist based on their first elements

if i have a list that looks something like (list a)如果我有一个类似于 (list a) 的列表

my_list=[[3, 1], [3, 0], [3, 0], [3, 1], [4, 1], [4, 0]]

i want to split that list into something like this (list b)我想将该列表拆分为这样的内容(列表 b)

my_list = [ [[3, 1], [3, 0], [3, 0], [3, 1]] , [[4, 1], [4, 0]] ]

as you can see, list b is sorted and grouped together by the values of the first element of the sublists.如您所见,列表 b 按子列表的第一个元素的值进行排序和分组。 The order of the pairs doesn't change.对的顺序不会改变。

thank you!谢谢你!

You can use itertools.groupby to group on the first element:您可以使用itertools.groupby对第一个元素进行分组:

my_list = itertools.groupby(my_list, key = lambda e: e[0])

This will give you an itertools.groupby generator object of (key, list) pairs.这将为您提供 (key, list) 对的itertools.groupby生成器对象。 Ignore the keys and convert it into a list by doing忽略键并将其转换为列表

[list(e[1]) for e in my_list]

This gives:这给出:

[[[3, 1], [3, 0], [3, 0], [3, 1]], [[4, 1], [4, 0]]]

itertools.groupby(...) should do the trick: itertools.groupby(...)应该可以解决问题:

import itertools

my_list=[[3, 1], [3, 0], [3, 0], [3, 1], [4, 1], [4, 0]]
#although your input seems to be sorted by 1st element I'll put it in here, in case if it wouldn't be
my_list=sorted(my_list, key=lambda x: x[0])

my_list=list(list(el) for k, el in itertools.groupby(my_list, key=lambda x: x[0]))

Output:输出:

[[[3, 1], [3, 0], [3, 0], [3, 1]], [[4, 1], [4, 0]]]

Ref: https://docs.python.org/2/library/itertools.html#itertools.groupby参考: https : //docs.python.org/2/library/itertools.html#itertools.groupby

my_list=[[3, 1], [3, 0], [3, 0], [3, 1], [4, 1], [4, 0]]
new_list =[]

j =0
for i in list(set([ value[0] for value in my_list])) :
    temp_list = []
    while (j < len(my_list)) and (my_list[j][0] == i):
        temp_list.append(my_list[j])
        j=j+1
    new_list.append(temp_list)


print (new_list)

[[[3, 1], [3, 0], [3, 0], [3, 1]], [[4, 1], [4, 0]]] [[[3, 1], [3, 0], [3, 0], [3, 1]], [[4, 1], [4, 0]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将列表分为相等的子列表,最后一个子列表由列表的第一个可能的元素组成 - How to split list into equal sublists with the last sublist made up from the first possible elements of the list 根据部分值将列表拆分为子列表 - Split list into sublist based on part of value 根据子列表python的大小将元素添加到列表 - Add elements to list based on size of sublist python 基于子列表中的第一个元素的列表的集群列表 - cluster list of lists based on the first element in the sublist 使用python将子列表中的第一个元素与同一列表中的元素合并 - Merging first element in sublist with elements in the same list using python 子列表中第一项的列表 - List of the first item in a sublist 根据不同列表的索引获取python中子列表的元素 - Get elements of a sublist in python based on indexes of a different list python根据子列表值重新排列列表中的元素 - python re-arrange elements in a list based on the sublist values 根据字符拆分列表元素 - Split elements of a list based on character 如何首先基于初始列表的单个元素将列表拆分为子列表,然后仅在 python 中将列表的连续部分拆分为子列表? - How to split a list into sublists based on single elements of the initial list first and then contiguous portions of the list simply in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM