python [:]的含义是什么 [英] What is the meaning of [:] in 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
问题是编写在
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屋!