Saturday, 23 July 2016

What is the meaning of [:] in python




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


Answer



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



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..


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...