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


Groups > comp.lang.python > #32606

Re: pythonic way

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'list?': 0.07; 'val': 0.07; 'english,': 0.09; 'lst': 0.09; 'index': 0.13; '7:58': 0.16; 'better?': 0.16; 'entry.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'idx,': 0.16; 'traverse': 0.16; 'wrote:': 0.17; 'replacing': 0.17; 'code.': 0.20; 'bit': 0.21; 'example': 0.23; 'thus': 0.24; 'tried': 0.25; 'header:In-Reply- To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'index,': 0.29; "i'm": 0.29; 'fri,': 0.30; 'operate': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'nov': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'explain': 0.36; "i'll": 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'think': 0.40
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=jI6sn9AbAy+u7r1Nj56QlVMA5FOWm+4zrHd2+JDbjng=; b=vWfkp6bZjDSLzDl7dYG2Q3zuU1VexfEnrTYpouXIIj9B7K2gt4QqJ7FKTLncIuk49I 2jjfLWwnvMOBWUHhPnTtQKEi/yWIBfUl8+zBidY40mXafOL+zFJka81DKFX0yeU1FANC /xiSHNWgr0xu2eMVPwGwrLxVp47PFt6ssSteHo3GB6Rbh8pJ8ag3d0FVOTTdo375FzBK ZRSMdx5Jm17wiwpr88Y4JvT91tdxpiJumFEEXfygveXu9aUqc+Nq6Wdh5N/jSJG0Ul5p hqeTsr7nnVplKv3HxHToIiR/EAxBdNhn4zkRWxvkPHwRMFLObOwWr8nB9OBhfhK395iV F3Dw==
MIME-Version 1.0
In-Reply-To <BLU0-SMTP91E1D6E7374A24FD3368B1F3670@phx.gbl>
References <CAFqGZREfq7fO4Okf1Oj9HBeUkWLYK_9=ho8N47_kyTKudGZSFA@mail.gmail.com> <CANaSqUfRdHoeUzcmAsmaK2WBgbXH63ZDu0pg2ebEX8APKkNwaw@mail.gmail.com> <BLU0-SMTP321F0666E2573FA6807D728F3670@phx.gbl> <CAPTjJmoNBRsK7YfNwEB2311LeA2Lc_oF0ufuhiAWm99pNcFqEA@mail.gmail.com> <BLU0-SMTP91E1D6E7374A24FD3368B1F3670@phx.gbl>
Date Fri, 2 Nov 2012 20:11:49 +1100
Subject Re: pythonic way
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.3188.1351847512.27098.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351847512 news.xs4all.nl 6891 [2001:888:2000:d::a6]:49987
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32606

Show key headers only | View raw


On Fri, Nov 2, 2012 at 7:58 PM, jack <naruto0.1@live.cn> wrote:
> thanks,but I don't think enumerate() is my want
> Have some ways to operate the reference of element,not a copy when I tried
> to traverse a list?
>
> I'm so sorry about my poor English, hope you don't mind it.

No probs, I'll be a little less vague and pointer-y and give you some
example code. :)

lst = ['foo', 'bar', 'quux', 'asdf', 'qwer', 'zxcv']
for idx, val in enumerate(lst):
    if val[1]=='w': lst[idx]='Replaced'
print(lst)

['foo', 'bar', 'quux', 'asdf', 'Replaced', 'zxcv']


Does that explain it a bit better? You get the index and can then
mutate the list using that index, thus replacing the original entry.

ChrisA

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


Thread

Re: pythonic way Chris Angelico <rosuav@gmail.com> - 2012-11-02 20:11 +1100

csiph-web