python - 如何找到列表中特定元素的位置?

标签 python list python-2.7

我有一个这样的列表:

 website =    ['http://freshtutorial.com/install-xamp-ubuntu/', 'http://linuxg.net/how-to-install-xampp-on-ubuntu-13-04-12-10-12-04/', 'http://ubuntuforums.org/showthread.php?t=2149654', 'http://andyhat.co.uk/2012/07/installing-xampp-32bit-ubuntu-11-10-12-04/', 'http://askubuntu.com/questions/303068/error-with-tar-command-cannot-install-xampp-1-8-1-on-ubuntu-13-04', 'http://askubuntu.com/questions/73541/how-to-install-xampp']

我想搜索以下列表是否包含某个 URL。

URL 将采用这种格式:url = 'http://freshtutorial.com'

网站是列表的第一个元素。因此,我想打印 1 不是 0

我希望循环中的所有内容,这样如果没有具有该 URL 的网站,它将再次运行并动态生成列表并再次搜索该网站。

到目前为止我已经这样做了:

for i in website:
    if url in website:
        print "True"

我似乎无法打印位置并将所有内容包装在循环中。另外,使用 regex 还是 if this in that 语法更好。谢谢

最佳答案

for i, v in enumerate(website, 1):
    if url in v:
        print i

关于python - 如何找到列表中特定元素的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17784090/

相关文章:

python - Pytorch 将张量转换为一个热

python - python 中的一切都是对象,但为什么不将关键字作为对象呢?

python - 如何将访问者重定向到登录页面?

list - 如何在erlang中将元组列表转换为二进制

python - 如何在 python 3 中获取小数列表

python - 删除嵌套列表中的重复项(不删除子列表中的重复元素)

python - 删除长子命令帮助输出?

python 2.7.8 : Different values of fromtimestamp on Windows and Linux

rundeckrun 的 Python 2.6 兼容性

python - 为多个时间步长值生成多个图