Search code examples
pythonlist-comprehensionpython-itertoolsenumerate

Using itertools for a condition with enumerate to only get certain list indexes (python)


This is my code:

from itertools import tee, islice, chain

def previous_and_next(some_iterable):
   prevs, items, nexts = tee(some_iterable, 3)
   prevs = chain([None], prevs)
   nexts = chain(islice(nexts, 1, None), [None])
   return zip(prevs, items, nexts)

fruits = ['watermelon', 'apple', 'apple', 'banana', 'kiwi', 'peach', 'apple',
          'pear', 'watermelon', 'apple', 'apple', 'orange', 'apple', 'grape']

nr_of_apples = 0
apples = []

for previous, item, nxt in previous_and_next(fruits):
    apple_indexes = [i for i, x in enumerate(fruits) if x == 'apple' and nxt != 'apple']
print(apple_indexes)

for i in apple_indexes:
    index = i - 1
    for previous, item, nxt in previous_and_next(fruits[index:]):
        if nxt != 'apple':
            break
        apples.append(nxt)

nr_of_apples = len(apples)

print(nr_of_apples)

I'm trying to count the number of times the word 'apples' appears in the list using itertools. I'm aware that this is a complex way of doing something that could be achieved in this, much simpler, way:

for f in fruits:
    if f == 'apple':
        apples.append(f)

But the idea here is to expand this code for a much more complex use with Stanford CoreNLP's Named Entity Recognition. So I'm starting simple and building my way up to that.

The issue is that my code is currently returning this:

[1, 2, 6, 9, 10, 12]  # indexes of the apples
8  # number of apples

Obviously there aren't 8 apples in the list, only 6. So my question is, how can I add a condition into the enumerate function to only get the indexes of the apples that aren't following another apple? So the output should look like this:

[1, 6, 9, 12]
6

Solution

  • try something like this,

    In [160]: list_of_index = [i for i,j in enumerate(fruits) if j == 'apple']
    
    In [161]: print list(set([min(i) if i[1] - i[0] == 1 else max(i) for i in zip(list_of_index,list_of_index[1:])]))
    [1, 12, 6, 9]
    
    In [162]: print fruits.count('apple')
    6