Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106262
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Michael Selik <michael.selik@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Drowning in a teacup? |
| Date | Fri, 01 Apr 2016 20:46:19 +0000 |
| Lines | 66 |
| Message-ID | <mailman.332.1459543597.28225.python-list@python.org> (permalink) |
| References | <ndmlj2$pb0$1@gioia.aioe.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de xqhd1KbtYblVkJamI7RcdgBa6knn1Ln5yZTnc53105Hg== |
| Return-Path | <michael.selik@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.012 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'python,': 0.02; 'matches': 0.07; 'strings.': 0.07; 'way:': 0.09; 'def': 0.13; '2016': 0.16; 'email addr:hotmail.com>': 0.16; 'key):': 0.16; 'keyword,': 0.16; 'mylist': 0.16; 'nope,': 0.16; 'proceeding': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'element': 0.18; 'variable': 0.18; 'to:2**1': 0.21; 'pass': 0.22; 'elements': 0.23; 'skip:b 30': 0.24; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'order.': 0.27; 'parameters': 0.27; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.30; 'fixed': 0.31; 'anyone': 0.32; 'url:python': 0.33; 'right?': 0.33; 'url:listinfo': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'beginning': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'thought': 0.37; 'list.': 0.37; 'doing': 0.38; 'front': 0.38; 'received:209': 0.38; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'called': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'here.': 0.62; 'skip:\xc2 10': 0.67; 'contrary': 0.72; 'snap': 0.84; 'words):': 0.84; 'wrong!': 0.84; 'notorious': 0.91; 'to:addr:hotmail.com': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=rlHVNbzICkJD636qIn3yzOsAe0Gwntjh5zlF9NH7aHg=; b=a22Y7XERYWVmkvM4sICUUOm8Jyb/P7ewlnHPXaayL0sNe+hHCRxkSCNQdWENiQzZCz Ec0I4ld3v5auFjVadQeVn6SWG4e8+cH+mffy5wmKdGaSXZWVgO/02fMRIO6piJbCf11K tfHWqreZ7VwCktsVkT6MUfvoY7HAqBVBfTJmJeAJwmjiWNw/zRxCbhcsf9R/O8cydcGF hDbA+OQkNWhuv8VD1PX4fp5tn5yb4UlT1EGWKqS0ey+4K8aAXbeDQexh8ndw52AIqcIE dM4fMGpkbmX/jQCieTRTAW3AjI6jOffPUsOGezhWPkTLDWl1p7EHH5kOI5R6/7UGzlPA 8nlg== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=rlHVNbzICkJD636qIn3yzOsAe0Gwntjh5zlF9NH7aHg=; b=P+N3h9JzliQG8xWTcT974LO2NdiLMUL3aZ1rSN76ZR5/eYaJaGaAj7FZshzwKvMHe0 81JZvSHpx8xlkc2e4IVDHyqEGkQg6FiGO1y3jyNJTiZauX5JxFyiJw9Bl6Vt6ye71wiA 9FPkTpUOyt53+lecY/O5gbJH8GyO4/O6X5djmnyfeI+w9kL3DZIuGIz6rsCIY/WkTaLV cEJ5lUdAEMgoTaJpG35P230YF1PlKMT/wDjNiIQkbpLRdKvlKUyZYMDRCEMs0tENeISj 8jtCGKsMEfkLHr6ejvcCYzljBP9L4utR71j0R9PgW5IAsSwmovHT+XtbDQpQkyWJwxMZ 9FJg== |
| X-Gm-Message-State | AD7BkJL1APqk4Q9b/sncw40h7YzD4Txh15wUGpbhMKUMpuASrqWkr7H+nRTc3jphu2m7q1XqQkG3P3atEZGDWw== |
| X-Received | by 10.140.237.204 with SMTP id i195mr9980662qhc.55.1459543588457; Fri, 01 Apr 2016 13:46:28 -0700 (PDT) |
| In-Reply-To | <ndmlj2$pb0$1@gioia.aioe.org> |
| X-Content-Filtered-By | Mailman/MimeDel 2.1.21 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| 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:106262 |
Show key headers only | View raw
Give this a shot
def snap(keyword, words):
matches = [i for i, s in enumerate(words) if s.startswith(keyword)]
for i in matches:
lst.insert(0, lst.pop(i))
Your current implementation is reassigning the local variable ``mylist`` to
a new list inside the function.
On Fri, Apr 1, 2016 at 4:30 PM Fillmore <fillmore_remove@hotmail.com> wrote:
>
> notorious pass by reference vs pass by value biting me in the backside
> here. Proceeding in order.
>
> I need to scan a list of strings. If one of the elements matches the
> beginning of a search keyword, that element needs to snap to the front
> of the list.
> I achieved that this way:
>
>
> for i in range(len(mylist)):
> if(mylist[i].startswith(key)):
> mylist = [mylist[i]] + mylist[:i] + mylist[i+1:]
>
> Since I need this code in multiple places, I placed it inside a function
>
> def bringOrderStringToFront(mylist, key):
>
> for i in range(len(mylist)):
> if(mylist[i].startswith(key)):
> mylist = [mylist[i]] + mylist[:i] + mylist[i+1:]
>
> and called it this way:
>
> if orderstring:
> bringOrderStringToFront(Tokens, orderstring)
>
> right?
> Nope, wrong! contrary to what I thought I had understood about how
> parameters are passed in Python, the function is acting on a copy(!) and
> my original list is unchanged.
>
> I fixed it this way:
>
> def bringOrderStringToFront(mylist, key):
>
> for i in range(len(mylist)):
> if(mylist[i].startswith(key)):
> mylist = [mylist[i]] + mylist[:i] + mylist[i+1:]
> return(mylist)
>
> and:
>
> if orderstring:
> Tokens = bringOrderStringToFront(Tokens, orderstring)
>
> but I'm left with a sour taste of not understanding what I was doing
> wrong. Can anyone elaborate? what's the pythonista way to do it right?
>
> Thanks
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Drowning in a teacup? Fillmore <fillmore_remove@hotmail.com> - 2016-04-01 16:27 -0400
Re: Drowning in a teacup? Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-01 20:39 +0000
Re: Drowning in a teacup? Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-01 14:52 -0600
Re: Drowning in a teacup? MRAB <python@mrabarnett.plus.com> - 2016-04-01 21:45 +0100
Re: Drowning in a teacup? Michael Selik <michael.selik@gmail.com> - 2016-04-01 20:46 +0000
Re: Drowning in a teacup? Steven D'Aprano <steve@pearwood.info> - 2016-04-02 09:46 +1100
Re: Drowning in a teacup? Fillmore <fillmore_remove@hotmail.com> - 2016-04-02 00:03 -0400
Re: Drowning in a teacup? Vito De Tullio <vito.detullio@gmail.com> - 2016-04-02 07:45 +0200
Re: Drowning in a teacup? Michael Selik <michael.selik@gmail.com> - 2016-04-02 05:51 +0000
Re: Drowning in a teacup? Vito De Tullio <vito.detullio@gmail.com> - 2016-04-02 08:26 +0200
Re: Drowning in a teacup? Peter Otten <__peter__@web.de> - 2016-04-02 12:31 +0200
Re: Drowning in a teacup? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-04-02 12:53 +0100
Re: Drowning in a teacup? Michael Selik <michael.selik@gmail.com> - 2016-04-02 14:36 +0000
Re: Drowning in a teacup? Ned Batchelder <ned@nedbatchelder.com> - 2016-04-02 12:28 -0700
Re: Drowning in a teacup? Random832 <random832@fastmail.com> - 2016-04-02 15:54 -0400
Re: Drowning in a teacup? Steven D'Aprano <steve@pearwood.info> - 2016-04-03 12:33 +1000
Re: Drowning in a teacup? Ethan Furman <ethan@stoneleaf.us> - 2016-04-02 16:15 -0700
Re: Drowning in a teacup? Random832 <random832@fastmail.com> - 2016-04-02 19:19 -0400
csiph-web