python_list.pop()的用法

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

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,删除最后一个列表值。

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items) - 1] def size(self): return len(self.items) def infix_to_postfix(infix_expr): prec = {} prec["*"] = 3 prec["/"] = 3 prec["+"] = 2 prec["-"] = 2 prec["("] = 1 op_stack = Stack() postfix_list = [] token_list = infix_expr.split() for token in token_list: if token.isdigit(): postfix_list.append(token) elif token == '(': op_stack.push(token) elif token == ')': top_token = op_stack.pop() while top_token != '(': postfix_list.append(top_token) top_token = op_stack.pop() else: while (not op_stack.isEmpty()) and (prec[op_stack.peek()] >= prec[token]): postfix_list.append(op_stack.pop()) op_stack.push(token) while not op_stack.isEmpty(): postfix_list.append(op_stack.pop()) return " ".join(postfix_list) def evaluate_postfix(postfix_expr): operand_stack = Stack() token_list = postfix_expr.split() for token in token_list: if token.isdigit(): operand_stack.push(int(token)) else: operand2 = operand_stack.pop() operand1 = operand_stack.pop() result = do_math(token, operand1, operand2) operand_stack.push(result) return operand_stack.pop() def do_math(op, op1, op2): if op == "*": return op1 * op2 elif op == "/": return op1 / op2 elif op == "+": return op1 + op2 else: return op1 - op2 infix_expr = "3 + 4 * 2" postfix_expr = infix_to_postfix(infix_expr) result = evaluate_postfix(postfix_expr) print(f"Infix: {infix_expr}") print(f"Postfix: {postfix_expr}") print(f"Result: {result}") 详细解读分析代码
最新发布
10-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值