python中[:]是什么意思 [英] What is the meaning of [:] in python

查看:25
本文介绍了python中[:]是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面代码中的 del taglist[:] 行有什么作用?

What does the line del taglist[:] do in the code below?

import urllib
from bs4 import BeautifulSoup
taglist=list()
url=raw_input("Enter URL: ")
count=int(raw_input("Enter count:"))
position=int(raw_input("Enter position:"))
for i in range(count):
    print "Retrieving:",url
    html=urllib.urlopen(url).read()
    soup=BeautifulSoup(html)
    tags=soup('a')
    for tag in tags:
        taglist.append(tag)
    url = taglist[position-1].get('href', None)
    del taglist[:]
print "Retrieving:",url

问题是编写一个在 http://www.pythonlearn.com 上扩展的 Python 程序/code/urllinks.py.程序将使用 urllib 从下面的数据文件中读取 HTML,从锚标签中提取 href= vaues,扫描相对于第一个特定位置的标签列表中的姓名,点击该链接并重复该过程多次并报告您找到的姓氏".示例问题:从 http://python-data.dr-chuck.net/known_by_Fikret 开始.html找到位置 3 处的链接(名字为 1).按照那个链接.重复此过程 4 次.答案是您检索到的姓氏.姓名顺序:Fikret Montgomery Mhairade Butchi Anayah依次姓氏:Anayah

The question is "write a Python program that expands on http://www.pythonlearn.com/code/urllinks.py. The program will use urllib to read the HTML from the data files below, extract the href= vaues from the anchor tags, scan for a tag that is in a particular position relative to the first name in the list, follow that link and repeat the process a number of times and report the last name you find". Sample problem: Start at http://python-data.dr-chuck.net/known_by_Fikret.html Find the link at position 3 (the first name is 1). Follow that link. Repeat this process 4 times. The answer is the last name that you retrieve. Sequence of names: Fikret Montgomery Mhairade Butchi Anayah Last name in sequence: Anayah

推荐答案

[:] 是数组中每个元素的数组切片语法.

[:] is the array slice syntax for every element in the array.

这里的答案更深入地介绍了一般用途:解释 Python 的切片符号

This answer here goes more in depth of the general uses: Explain Python's slice notation

del arr # Deletes the array itself
del arr[:]  # Deletes all the elements in the array
del arr[2]  # Deletes the second element in the array
del arr[1:]  # etc..

这篇关于python中[:]是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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