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
以下是对实现栈的应用处理加减乘除运算代码的详细解读,涵盖 `Stack` 类、中缀表达式转后缀表达式、后缀表达式求值等功能。 #### `Stack` 类 ```python 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) ``` - `__init__` 方法:这是类的构造函数,用于初始化一个空列表 `self.items`,该列表将作为栈的存储容器。 - `isEmpty` 方法:用于检查栈是否为空。若栈为空列表,则返回 `True`,否则返回 `False`。 - `push` 方法:将一个元素 `item` 添加到栈的顶部,通过列表的 `append` 方法实现。 - `pop` 方法:移除并返回栈顶元素,使用列表的 `pop` 方法,该方法默认移除并返回列表的最后一个元素。 - `peek` 方法:返回栈顶元素,但不移除它。通过访问列表的最后一个元素实现。 - `size` 方法:返回栈中元素的数量,即列表的长度。 #### 中缀表达式转后缀表达式 ```python 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) ``` - `prec` 字典:定义了运算符的优先级,`*` 和 `/` 的优先级为 3,`+` 和 `-` 的优先级为 2,`(` 的优先级为 1。 - `op_stack`:创建一个 `Stack` 类的实例,用于存储运算符。 - `postfix_list`:用于存储转换后的后缀表达式。 - `token_list`:将输入的中缀表达式按空格分割成一个个元素。 - 遍历 `token_list`: - 若元素是数字,直接添加到 `postfix_list` 中。 - 若元素是 `(`,将其压入 `op_stack`。 - 若元素是 `)`,从 `op_stack` 中弹出元素并添加到 `postfix_list` 中,直到遇到 `(`,`(` 不添加到 `postfix_list` 中。 - 若元素是运算符,将 `op_stack` 中优先级高于或等于该运算符的元素弹出并添加到 `postfix_list` 中,然后将该运算符压入 `op_stack`。 - 最后,将 `op_stack` 中剩余的元素全部弹出并添加到 `postfix_list` 中,将 `postfix_list` 转换为字符串返回。 #### 后缀表达式求值 ```python 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 ``` - `operand_stack`:创建一个 `Stack` 类的实例,用于存储操作数。 - `token_list`:将输入的后缀表达式按空格分割成一个个元素。 - 遍历 `token_list`: - 若元素是数字,将其转换为整数并压入 `operand_stack`。 - 若元素是运算符,从 `operand_stack` 中弹出两个操作数,调用 `do_math` 函数进行计算,将结果压入 `operand_stack`。 - 最后,返回 `operand_stack` 中唯一的元素,即表达式的计算结果。 ### 完整代码示例 ```python 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}") ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值