python [:]的含义是什么 [英] What is the meaning of [:] in python

查看:235
本文介绍了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/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天全站免登陆