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


Groups > comp.lang.python > #32606

Re: pythonic way

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 2012-11-02 20:11 +1100
Subject Re: pythonic way
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3188.1351847512.27098.python-list@python.org> (permalink)

Show all headers | 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