itertools py3.4 - filter list using not equal - fails as bool

Sayth Renshaw flebber.crue at gmail.com
Tue May 12 18:43:49 EDT 2015


why can't I filter a list based on an itertools condition using dropwhile?

This is the docs and the example. https://docs.python.org/3/library/itertools.html#itertools.dropwhile

def less_than_10(x):
    return x < 10

itertools.takewhile(less_than_10, itertools.count()) =>
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9

so I have a list I have created (converted from itertools). pm is permutations 


print(stats)
[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 2), (1, 3, 4), (1, 3, 5), (1, 4, 2), (1, 4, 3), (1, 4, 5), (1, 5, 2), (1, 5, 3), (1, 5, 4), (2, 1, 3), (2, 1, 4), (2, 1, 5), (2, 3, 1), (2, 3, 4), (2, 3, 5), (2, 4, 1), (2, 4, 3), (2, 4, 5), (2, 5, 1), (2, 5, 3), (2, 5, 4), (3, 1, 2), (3, 1, 4), (3, 1, 5), (3, 2, 1), (3, 2, 4), (3, 2, 5), (3, 4, 1), (3, 4, 2), (3, 4, 5), (3, 5, 1), (3, 5, 2), (3, 5, 4), (4, 1, 2), (4, 1, 3), (4, 1, 5), (4, 2, 1), (4, 2, 3), (4, 2, 5), (4, 3, 1), (4, 3, 2), (4, 3, 5), (4, 5, 1), (4, 5, 2), (4, 5, 3), (5, 1, 2), (5, 1, 3), (5, 1, 4), (5, 2, 1), (5, 2, 3), (5, 2, 4), (5, 3, 1), (5, 3, 2), (5, 3, 4), (5, 4, 1), (5, 4, 2), (5, 4, 3)]


I simply wanted to create an easy way to create summary stats of my stats list(poorly named). So in this case easy to check answers. so how many tuples in my list have a 1 in item[0] and how many don't. Then hoping to build on that for example how many have item[0] == 1 && (item[1] == 2 or item[1] == 4) etc.

I can achieve it via an else if but that would become ugly quick. 

for item in stats:
    if item[0] == 1: 
        nums += 1
    elif item[0] != 1:
        not_in += 1
    else:
        pass
    

myString = "i have {a} in and {b} so the percentage is {c}%".format(a=nums, b=not_in, c=(nums/(nums + not_in)))

I thought dropwhile from the docs appeared a good option but it returns bool.

answer = listb.extend(itertools.dropwhile(item[0] != 1, stats))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-130-70c93104d1c5> in <module>()
----> 1 answer = listb.extend(itertools.dropwhile(item[0] != 1, stats))

TypeError: 'bool' object is not callable

Think I am making this hard somewhere that it is actually easy. 

As an aside do I really need to convert the iterator object to a list to create and summarize iterator contents?

Currently I have created a function to achieve this as below.

def myGen(parcel):
    x = []
    [x.append(y) for y in parcel]
    return x

myPerm = pm(range(1,6),3)

stats = myGen(myPerm)



More information about the Python-list mailing list