Search code examples
pythonfunctional-programmingziplist-comprehensionpython-itertools

How to get the index and occurance of each item using itertools.groupby()


Here's the story I have two lists:

list_one=[1,2,9,9,9,3,4,9,9,9,9,2]
list_two=["A","B","C","D","A","E","F","G","H","Word1","Word2"]

I want to find the indicies of consecutive 9's in list_one so that I can get corresponding string from list_two, I've tried:

group_list_one= [(k, sum(1 for i in g),pdn.index(k)) for k,g in groupby(list_one)]

I was hoping to get the index of the first 9 in each tuple and then try to go from there, but that did not work..

What can I do here?? P.S.: I've looked at the documentation of itertools but it seems very vague to me.. Thanks in advance

EDIT: Expected output is (key,occurances,index_of_first_occurance) something like

[(9, 3, 2), (9, 4, 7)]

Solution

  • Judging by your expected output, give this a try:

    from itertools import groupby
    
    list_one=[1,2,9,9,9,3,4,9,9,9,9,2]
    list_two=["A","B","C","D","A","E","F","G","H","Word1","Word2"]
    data = zip(list_one, list_two)
    i = 0
    out = []
    
    for key, group in groupby(data, lambda x: x[0]):
            number, word = next(group)
            elems = len(list(group)) + 1
            if number == 9 and elems > 1:
                out.append((key, elems, i))
            i += elems
    
    print out
    

    Output:

    [(9, 3, 2), (9, 4, 7)]
    

    But if you really wanted an output like this:

    [(9, 3, 'C'), (9, 4, 'G')]
    

    then look at this snippet:

    from itertools import groupby
    
    list_one=[1,2,9,9,9,3,4,9,9,9,9,2]
    list_two=["A","B","C","D","A","E","F","G","H","Word1","Word2"]
    data = zip(list_one, list_two)
    out = []
    
    for key, group in groupby(data, lambda x: x[0]):
        number, word = next(group)
        elems = len(list(group)) + 1
        if number == 9 and elems > 1:
            out.append((key, elems, word))
    
    print out