For循环中的Python列表 [英] Python List in a For Loop

查看:95
本文介绍了For循环中的Python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,并且有一个关于使用for循环更新列表的问题.这是我的代码:

I am new to Python and had a question about updating a list using a for loop. Here is my code:

urls = ['http://www.city-data.com/city/javascript:l("Abbott");' , 'http://www.city-data.com/city/javascript:l("Abernathy");' ,

'http://www.city-data.com/city/Abilene-Texas.html' ,'http://www.city-data.com/city/javascript:l("Abram-Perezville");' ,  

'http://www.city-data.com/city/javascript:l("Ackerly");' , 'http://www.city-data.com/city/javascript:l("Adamsville");', 

'http://www.city-data.com/city/Addison-Texas.html']

for url in urls:
    if "javascript" in url:
        print url
        url = url.replace('javascript:l("','').replace('");','-Texas.html')
        print url

for url in urls:
    if "javascript" in url:
        url = url.replace('javascript:l("','').replace('");','-Texas.html')
print "\n"  
print urls

我使用了第一个for循环来检查语法是否正确,并且工作正常.但是第二个for循环是我想使用的代码,但是它不能正常工作.我该如何使用第二个for循环全局更新列表,以便可以在for循环外打印或存储更新的列表?

I used the first for loop to check if the syntax was correct, and it worked fine. But the second for loop is the code I want to use, but it's not working properly. How would I go about globally updating the list with the second for loop so I can print or store the updated list outside of the for loop?

推荐答案

您可以使用其索引更新列表项:

You can update the list items using their index:

for i, url in enumerate(urls):
    if "javascript" in url:
        urls[i] = url.replace('javascript:l("','').replace('");','-Texas.html')

另一种替代方法是使用列表理解:

Another alternative is to use a list comprehension:

def my_replace(s):
    return s.replace('javascript:l("','').replace('");','-Texas.html')

urls[:] = [my_replace(url) if "javascript" in url else url for url in urls]

此处urls[:]表示将urls列表中的所有项目替换为由列表理解创建的新列表.

Here urls[:] means replace all the items of urls list with the new list created by the list comprehension.

代码无法正常工作的原因是您将变量url分配给其他对象,并且将对象的引用之一更改为指向其他对象不会影响其他引用.因此,您的代码等效于:

The reason why your code didn't worked is that you're assigning the variable url to something else, and changing one of the reference of an object to point to something else doesn't affect the other references. So, your code is equivalent to:

>>> lis = ['aa', 'bb', 'cc']
>>> url = lis[0]                   #create new reference to 'aa'
>>> url = lis[0].replace('a', 'd') #now assign url to a new string that was returned by `lis[0].replace`
>>> url 
'dd'
>>> lis[0]
'aa'

还要注意,str.replace总是返回字符串的新副本,它永远不会更改原始字符串,因为字符串在Python中是不可变的.如果lis[0]是列表,并且您使用.append.extend等对其执行了任何就地操作,那么这也将影响原始列表.

Also note that str.replace always returns a new copy of string, it never changes the original string because strings are immutable in Python. In case lis[0] was a list and you performed any in-place operation on it using .append, .extend etc, then that would have affected the original list as well.

这篇关于For循环中的Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆