Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99953
| From | Jussi Piitulainen <harvesting@is.invalid> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: filter a list of strings |
| Date | 2015-12-03 13:53 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <lf5twnzsq8m.fsf@ling.helsinki.fi> (permalink) |
| References | <mailman.155.1449122975.14615.python-list@python.org> <lf51tb459f2.fsf@ling.helsinki.fi> <mailman.165.1449134847.14615.python-list@python.org> |
<c.buhtz@posteo.jp> writes:
> Thank you for your suggestion. This will help a lot.
>
> On 2015-12-03 08:32 Jussi Piitulainen wrote:
>> list = [ item for item in list
>> if ( 'Banana' not in item and
>> 'Car' not in item ) ]
>
> 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.
Others have given the crucial search word, "list comprehension".
The brackets are part of the notation. Without brackets, or grouped in
parentheses, it would be a generator expression, whose value would yield
the items on demand. Curly braces would make it a set or dict
comprehension; the latter also uses a colon.
> I understand this
> for x in y:
> if ...
>
> But what is about the 'x' in front of all that?
You can understand the notation as collecting the values from nested
for-loops and conditions, just like you are attempting here, together
with a fresh list that will be the result. The "x" in front can be any
expression involving the loop variables; it corresponds to a
result.append(x) inside the nested loops and conditions. Roughly:
result = []
for x in xs:
for y in ys:
if x != y:
result.append((x,y))
==>
result = [(x,y) for x in xs for y in ys if x != y]
On python.org, this information seems to be in the tutorial but not in
the language reference.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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