Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7013
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.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; 'else:': 0.03; 'string.': 0.04; '(except': 0.05; 'does.': 0.07; 'function,': 0.07; 'slice': 0.07; 'python': 0.08; '>>>>': 0.09; 'empty,': 0.09; 'loop.': 0.09; 'operator,': 0.09; 'similarly,': 0.09; 'string;': 0.09; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'angelico': 0.16; 'bit.': 0.16; 'collects': 0.16; 'compact': 0.16; 'evaluates': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'handy': 0.16; 'iteration,': 0.16; 'redone': 0.16; 'string:': 0.16; 'terminates': 0.16; 'ternary': 0.16; 'zero:': 0.16; 'slightly': 0.19; 'header:In-Reply-To:1': 0.21; 'subject:question': 0.23; 'trying': 0.23; 'division': 0.23; 'least,': 0.23; 'received:209.85.210.174': 0.23; 'received:mail- iy0-f174.google.com': 0.23; "doesn't": 0.25; 'function': 0.25; 'skip:[ 10': 0.26; 'string': 0.26; "i'm": 0.27; 'message- id:@mail.gmail.com': 0.28; 'work:': 0.29; 'version': 0.29; 'code,': 0.29; 'bit': 0.30; 'array': 0.30; 'strings,': 0.30; 'strings.': 0.30; 'sun,': 0.30; 'looks': 0.31; 'seem': 0.32; "can't": 0.32; "skip:' 10": 0.32; 'someone': 0.33; 'does': 0.33; 'to:addr:python-list': 0.33; 'chris': 0.34; 'characters': 0.34; 'else': 0.35; 'certain': 0.36; 'takes': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'case': 0.37; 'could': 0.38; 'anything': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'skip:s 20': 0.39; 'received:209': 0.39; 'version:': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'allows': 0.40; 'totally': 0.40; 'simply': 0.60; 'hope': 0.60; 'huge': 0.62; 'special': 0.66; 'imagine': 0.72; 'clearer': 0.84; 'numbered': 0.84; "\xa0i'm": 0.84; 'gaps': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=VA90+7/E/hSonUh3MI0ROJVF9NiBXgmAabXsc2xtDVE=; b=OpBl3pVYgbx7YZrR9472pyeaix323WMTi41fFDR4q7nVqo6/UGu1A7vBN4LykaF1ZO WU4gr4bzAGJcqVBf9H0djOkOEvHxBIQbc7kzMUIl3ZHplE9X7vVPxymFJQNPna2yxvLz ik4N1qbDFNWda1MmsM0UTRBLAHEbXxfVuAtM8= |
| DomainKey-Signature | a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=e0E5sxYdKDiry+oE5dXKwZQKhhB661vmrx46d9HzZ8WbpU8z6zfTcBSKxvXE3AYn8P 2D0nPvzcNU69Uo4s/apqIWbDlzQzF6FRN64m4eW5NmrefxrfEt2ABG6SmnKOWhqjZQ+P R4z15mQCZ3l2NVHhq3P3CAe9z5r+WCkqoW2Rg= |
| MIME-Version | 1.0 |
| In-Reply-To | <20110604174624.V8FO6.213264.root@cdptpa-web07-z02> |
| References | <20110604174624.V8FO6.213264.root@cdptpa-web07-z02> |
| Date | Sun, 5 Jun 2011 04:09:07 +1000 |
| Subject | Re: Lambda question |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2455.1307210950.9059.python-list@python.org> (permalink) |
| Lines | 58 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1307210951 news.xs4all.nl 49042 [::ffff:82.94.164.166]:37982 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:7013 |
Show key headers only | View raw
On Sun, Jun 5, 2011 at 3:46 AM, <jyoung79@kc.rr.com> wrote:
> It doesn't work with a huge list, but looks like it could be handy in certain
> circumstances. I'm trying to understand this code, but am totally lost. I
> know a little bit about lambda, as well as the ternary operator, but how
> does this part work:
>
>>>> f('dude'[3:], 3, []+[('dude'[:3])])
> ['dud', 'e']
First, the easy bit. 'dude'[3:] is a slice operation on the string;
same with 'dude'[:3]. Imagine the gaps between the letters as being
numbered from zero:
| d | u | d | e |
0 1 2 3 4
'dude'[3:] means "take the string 'dude', start at position 3, and go
to the end of the string" - so that's just the letter "e". Similarly,
'dude'[:3] means "take the string 'dude', start at the beginning, and
go to position 3" - so that's "dud".
Here's a version of that function, redone in a slightly less compact form:
def strsplit(string,n,acc=[]):
if string:
return strsplit(string[n:],n,acc+[string[:n]])
else:
return acc
Yes, it's recursive. In each iteration, until the string is empty, it
takes the first n characters and puts them in the accumulator list,
and then trims those n off and leaves them in the string. Here's a
non-recursive version:
def strsplit(string,n):
acc=[]
while string:
acc.append(string[:n])
string=string[n:]
return acc
This might make it a bit clearer what it does. The accumulator
collects ("accumulates") short strings, the string gets progressively
snipped, and once the string is empty, it evaluates as False and
terminates the loop.
Python doesn't seem to have an inbuilt function to divide strings in
this way. At least, I can't find it (except the special case where n
is 1, which is simply 'list(string)'). Pike allows you to use the
division operator: "Hello, world!"/3 is an array of 3-character
strings. If there's anything in Python to do the same, I'm sure
someone else will point it out.
Hope that helps!
Chris Angelico
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Lambda question Chris Angelico <rosuav@gmail.com> - 2011-06-05 04:09 +1000
csiph-web