On 2015-05-12 23:43, Sayth Renshaw wrote:
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

That evaluates:

    item[0] != 1

which returns True or False. Apparently, "item" happens to be bound to
something that can be subscripted.

It then calls:

    itertools.dropwhile(..., stats)

passing the bool as the first argument (the predicate) of dropwhile.

When it tries to test the first item from stats, it discovers that the
predicate is not callable; it's a bool.

Result: TypeError!

And another point: a list's "extend" method modifies the list and then
returns None, so "answer" will be None.

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 = []

Don't use a list comprehension like this:

     [x.append(y) for y in parcel]

Do this instead:

    for y in parcel:
        x.append(y)

or, alternatively, this:

    x.extend(parcel)

     return x

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

stats = myGen(myPerm)


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to