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


Groups > comp.lang.python > #99951

Re: filter a list of strings

From jmp <jeanmichel@sequans.com>
Newsgroups comp.lang.python
Subject Re: filter a list of strings
Date 2015-12-03 11:03 +0100
Message-ID <mailman.169.1449137041.14615.python-list@python.org> (permalink)
References <mailman.155.1449122975.14615.python-list@python.org> <lf51tb459f2.fsf@ling.helsinki.fi> <3pBBdn3nVcz5vP8@dovecot03.posteo.de>

Show all headers | View raw


On 12/03/2015 10:27 AM, c.buhtz@posteo.jp wrote:
> I often saw constructions like this
>    x for x in y if ...
> But I don't understand that combination of the Python keywords (for,
> in, if) I allready know. It is to complex to imagine what there really
> happen.
>
> I understand this
>    for x in y:
>      if ...
>
> But what is about the 'x' in front of all that?
>

I'd advise you insist on understanding this construct as it is a very 
common (and useful) construct in python. It's a list comprehension, you 
can google it to get some clues about it.

consider this example
[2*i for i in [0,1,2,3,4] if i%2] == [2,6]

you can split it in 3 parts:
1/ for i in [0,1,2,3,4]
2/ if i/2
3/ 2*i

1/ I'm assuming you understand this one
2/ this is the filter part
3/ this is the mapping part, it applies a function to each element


To go back to your question "what is about the 'x' in front of all 
that". The x  is the mapping part, but the function applied is the 
function identity which simply keeps the element as is.

# map each element, no filter
[2*i for i in [0,1,2,3,4]] == [0, 2, 4, 6, 8]

# no mapping, keeping only odd elements
[i for i in [0,1,2,3,4] if i%2] == [1,3]

JM

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


Thread

filter a list of strings <c.buhtz@posteo.jp> - 2015-12-03 02:15 +0100
  Re: filter a list of strings Jussi Piitulainen <harvesting@is.invalid> - 2015-12-03 08:32 +0200
    Re: filter a list of strings <c.buhtz@posteo.jp> - 2015-12-03 10:27 +0100
      Re: filter a list of strings Jussi Piitulainen <harvesting@is.invalid> - 2015-12-03 13:53 +0200
      Re: filter a list of strings Peter Pearson <pkpearson@nowhere.invalid> - 2015-12-05 19:42 +0000
    Re: filter a list of strings Chris Angelico <rosuav@gmail.com> - 2015-12-03 20:40 +1100
    Re: filter a list of strings Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2015-12-03 10:46 +0100
    Re: filter a list of strings Laura Creighton <lac@openend.se> - 2015-12-03 10:53 +0100
    Re: filter a list of strings jmp <jeanmichel@sequans.com> - 2015-12-03 11:03 +0100
    Re: filter a list of strings Peter Otten <__peter__@web.de> - 2015-12-03 11:13 +0100
    Re: filter a list of strings Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-03 14:16 +0000
      Re: filter a list of strings Jussi Piitulainen <harvesting@is.invalid> - 2015-12-03 17:02 +0200
  Re: filter a list of strings Grobu <snailcoder@retrosite.invalid> - 2015-12-03 13:17 +0100

csiph-web