简体   繁体   English

Python:基于连续的相同值将列表拆分为索引

[英]Python: split list into indices based on consecutive identical values

If you could advice me how to write the script to split list by number of values I mean: 如果您可以建议我如何编写脚本以按值数目拆分列表,我的意思是:

my_list =[11,11,11,11,12,12,15,15,15,15,15,15,20,20,20] 

And there are 11-4,12-2,15-6,20-3 items. 并且有11-4,12-2,15-6,20-3个项目。 So in next list for exsample range(0:100) I have to split on 4,2,6,3 parts So I counted same values and function for split list, but it doen't work with list: 因此,在下一个示例范围(0:100)的列表中,我必须分割4,2,6,3个部分,因此我为分割列表计算了相同的值和功能,但不适用于列表:

 div=Counter(my_list).values() ##counts same values in the list

 def chunk(it, size):
    it = iter(it)
    return iter(lambda: tuple(islice(it, size)), ())

What do I need: 我需要什么:

Out: ([0,1,2,3],[4,5],[6,7,8,9,10,11], etc...] 

You can use enumerate , itertools.groupby , and operator.itemgetter : 您可以使用enumerateitertools.groupbyoperator.itemgetter

In [45]: import itertools

In [46]: import operator

In [47]: [[e[0] for e in d[1]] for d in itertools.groupby(enumerate(my_list), key=operator.itemgetter(1))]
Out[47]: [[0, 1, 2, 3], [4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14]]

What this does is as follows: 其作用如下:

  1. First it enumerates the items. 首先,它枚举项目。

  2. It groups them, using the second item in each enumeration tuple (the original value). 它使用每个枚举元组中的第二项(原始值)对它们进行分组。

  3. In the resulting list per group, it uses the first item in each tuple (the enumeration) 在每个组的结果列表中,它使用每个元组中的第一项(枚举)

Solution in Python 3 , If you are only using counter : Python 3中的解决方案,如果仅使用counter

from collections import Counter
my_list =[11,11,11,11,12,12,15,15,15,15,15,15,20,20,20] 
count = Counter(my_list)
div= list(count.keys())         # take only keys
div.sort()
l = []
num = 0
for i in div:
    t = []
    for j in range(count[i]):   # loop number of times it occurs in the list
        t.append(num)
        num+=1
    l.append(t)
print(l)

Output: 输出:

[[0, 1, 2, 3], [4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14]]

Alternate Solution using set : 使用set替代解决方案:

my_list =[11,11,11,11,12,12,15,15,15,15,15,15,20,20,20] 
val = set(my_list)                     # filter only unique elements
ans = []
num = 0
for i in val:
    temp = []
    for j in range(my_list.count(i)):   # loop till number of occurrence of each unique element
        temp.append(num)
        num+=1
    ans.append(temp)
print(ans)

EDIT: As per required changes made to get desired output as mention in comments by @Protoss Reed 编辑:根据所需的更改以获取所需的输出,如@Protoss Reed在评论中所述

my_list =[11,11,11,11,12,12,15,15,15,15,15,15,20,20,20] 
val = list(set(my_list))                     # filter only unique elements
val.sort()                                   # because set is not sorted by default
ans = []
index = 0
l2 = [54,21,12,45,78,41,235,7,10,4,1,1,897,5,79]
for i in val:
    temp = []
    for j in range(my_list.count(i)):   # loop till number of occurrence of each unique element
        temp.append(l2[index])
        index+=1
    ans.append(temp)
print(ans)

Output: 输出:

[[54, 21, 12, 45], [78, 41], [235, 7, 10, 4, 1, 1], [897, 5, 79]]

Here I have to convert set into list because set is not sorted and I think remaining is self explanatory. 在这里,我必须将set转换为list因为set没有排序,我认为剩下的是不言而喻的。

Another Solution if input is not always Sorted (using OrderedDict ): 如果输入不总是Sorted(使用OrderedDict ),则另一个解决方案

from collections import OrderedDict
v = OrderedDict({})
my_list=[12,12,11,11,11,11,20,20,20,15,15,15,15,15,15]
l2 = [54,21,12,45,78,41,235,7,10,4,1,1,897,5,79]
for i in my_list:                # maintain count in dict
    if i in v:
        v[i]+=1
    else:
        v[i]=1
ans =[]
index = 0
for key,values in v.items():
    temp = []
    for j in range(values):
        temp.append(l2[index])
        index+=1
    ans.append(temp)
print(ans)

Output: 输出:

[[54, 21], [12, 45, 78, 41], [235, 7, 10], [4, 1, 1, 897, 5, 79]]

Here I use OrderedDict to maintain order of input sequence which is random(unpredictable) in case of set . 在这里,我使用OrderedDict来维护输入序列的顺序,在set情况下该顺序是随机的(不可预测的)。

Although I prefer @Ami Tavory's solution which is more pythonic. 虽然我更喜欢@Ami Tavory的解决方案,但它更像pythonic。

[Extra work: If anybody can convert this solution into list comprehension it will be awesome because i tried but can not convert it to list comprehension and if you succeed please post it in comments it will help me to understand] [额外工作:如果有人可以将此解决方案转换为list comprehension那将很棒,因为我尝试了但无法将其转换为list comprehension ,如果成功,请在评论中发布它,这将有助于我理解。]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM