Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99951
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | jmp <jeanmichel@sequans.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: filter a list of strings |
| Date | Thu, 03 Dec 2015 11:03:48 +0100 |
| Lines | 43 |
| 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> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de bzCnooddhuvoAtuF48+srgXWawxo+Sg+3eVr396Re9mw== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'that?': 0.05; '[0,': 0.09; 'happen.': 0.09; 'part,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'python.': 0.11; 'applies': 0.15; '2*i': 0.16; 'clues': 0.16; 'element,': 0.16; 'mapping,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'element': 0.18; 'odd': 0.18; 'assuming': 0.22; 'am,': 0.23; 'elements': 0.23; 'split': 0.23; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'question': 0.27; 'function': 0.28; "i'm": 0.30; "i'd": 0.31; 'common': 0.33; 'combination': 0.33; 'know.': 0.34; 'list': 0.34; 'filter': 0.35; 'identity': 0.35; 'mapping': 0.35; 'but': 0.36; 'there': 0.36; '(and': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'front': 0.38; 'google': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'received:194': 0.61; 'back': 0.62; 'charset:windows-1252': 0.62; 'is.': 0.63; 'saw': 0.77; 'construct': 0.84; 'imagine': 0.96 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | paris.sequans.com |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 |
| In-Reply-To | <3pBBdn3nVcz5vP8@dovecot03.posteo.de> |
| 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> |
| Xref | csiph.com comp.lang.python:99951 |
Show key headers only | 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 | 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