python_list.pop()的用法

本文通过一个具体实例展示了如何使用Python中的列表及其pop方法来实现网页抓取任务中的URL管理。通过构造URL列表并利用多线程下载图片,文章详细解释了列表pop方法的应用,即从列表中移除并获取指定位置的元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.遇到了这个问题

cur_page = queue.pop(0)

整段代码是这样的

def main():
   create_dir('pic') # 创建主文件夹
   queue = [i for i in range(1, 72)]   # 构造url链接的页码列表
   threads = []
   while len(queue) > 0:
       for thread in threads:
           if not thread.is_alive():
               threads.remove(thread)
       while len(threads) < 5 and len(queue) > 0:   # 最大线程数设置为 5
           cur_page = queue.pop(0) # 移除列表中第一个元素,并返回该元素的值
           url = 'http://meizitu.com/a/more_{}.html'.format(cur_page)
           thread = threading.Thread(target=execute, args=(url,)) # 传入excute函数
           thread.setDaemon(True)# 设置线程为守护线程
           thread.start()
           print('{}正在下载{}页'.format(threading.current_thread().name, cur_page))
           threads.append(thread)# 把线程从线程列表中删除

2.用法

pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。

cur_page = queue.pop(0)

该处是移除列表中第一个元素,并返回该元素的值

cur_page = queue.pop() # 默认不传值的时候

默认为 index=-1,删除最后一个列表值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值