Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90504

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

Path csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!1.eu.feeder.erje.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'else:': 0.03; 'argument': 0.05; 'elif': 0.05; 'none,': 0.07; 'tries': 0.07; 'ugly': 0.07; '3),': 0.09; 'false.': 0.09; 'modifies': 0.09; 'percentage': 0.09; 'subject:using': 0.09; 'def': 0.12; '"item"': 0.16; '(1,': 0.16; '(2,': 0.16; '(3,': 0.16; '(converted': 0.16; '1),': 0.16; '2),': 0.16; '3)]': 0.16; '4),': 0.16; '5),': 0.16; 'bool': 0.16; 'callable': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'instead:': 0.16; 'item[0]': 0.16; 'iterator': 0.16; 'itertools': 0.16; 'last)': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'none.': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'stats': 0.16; 'subject:fails': 0.16; 'summarize': 0.16; 'typeerror:': 0.16; 'x.append(y)': 0.16; 'wrote:': 0.18; 'passing': 0.19; '(the': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'example.': 0.24; 'skip:{ 20': 0.24; 'this:': 0.26; 'pass': 0.26; 'somewhere': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'subject:list': 0.30; 'tuples': 0.31; 'summary': 0.32; 'option': 0.32; 'another': 0.32; 'url:python': 0.33; '(most': 0.33; "can't": 0.35; 'created': 0.35; 'something': 0.35; 'convert': 0.35; 'received:84': 0.35; 'test': 0.35; 'but': 0.35; 'really': 0.36; 'method': 0.36; 'url:org': 0.36; 'list': 0.37; 'filter': 0.38; 'url:library': 0.38; 'to:addr :python-list': 0.38; 'recent': 0.39; 'aside': 0.39; 'to:addr:python.org': 0.39; 'skip:x 10': 0.40; 'how': 0.40; 'easy': 0.60; 'url:3': 0.61; 'simply': 0.61; 'first': 0.61; 'making': 0.63; 'become': 0.64; 'below.': 0.71; 'hoping': 0.75; 'discovers': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=dZcO3Bne c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=SY9aFQKHIdAA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=Dg1DVHxstwU45qw_U_IA:9 a=QEXdDO2ut3YA:10 a=zAaNSeJdLYgA:10
X-AUTH mrabarnett@:2500
Date Wed, 13 May 2015 00:22:42 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: itertools py3.4 - filter list using not equal - fails as bool
References <05defef5-74aa-4a5d-b7e7-9b521512152c@googlegroups.com>
In-Reply-To <05defef5-74aa-4a5d-b7e7-9b521512152c@googlegroups.com>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.425.1431472972.12865.python-list@python.org> (permalink)
Lines 93
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1431472972 news.xs4all.nl 2906 [2001:888:2000:d::a6]:48641
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90504

Show key headers only | View raw


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)
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

itertools py3.4 - filter list using not equal - fails as bool Sayth Renshaw <flebber.crue@gmail.com> - 2015-05-12 15:43 -0700
  Re: itertools py3.4 - filter list using not equal - fails as bool MRAB <python@mrabarnett.plus.com> - 2015-05-13 00:22 +0100
  Re: itertools py3.4 - filter list using not equal - fails as bool Peter Otten <__peter__@web.de> - 2015-05-13 09:40 +0200
    Re: itertools py3.4 - filter list using not equal - fails as bool Sayth Renshaw <flebber.crue@gmail.com> - 2015-05-13 06:58 -0700

csiph-web