Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: pythonic way (http://www.velocityreviews.com/forums/t954138-re-pythonic-way.html)

Chris Angelico 11-02-2012 09:11 AM

Re: pythonic way
 
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


All times are GMT. The time now is 08:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.