小编Tan*_*mah的帖子

如何使用索引向后枚举Python中的列表(从末尾到开头)

我想迭代从 Digikey api 接收到的数据列表,任何谷歌搜索都没有太多运气,因为该功能enumerate(reversed(part.standard_pricing))不起作用,并且part.standard_pricing由于某种原因您无法逆转类似的事情是需要的:

for b_idx in range(part.standard_pricing, -1, -1):
            print(f"Test at: {b_idx} , Q: {part.standard_pricing[b_idx].break_quantity}")
            if PCountQ >= part.standard_pricing[b_idx].break_quantity:
                parts_df['PRICE_PER_PCB_Q'][idx] = part.standard_pricing[b_idx].unit_price * PCountQ
                break
Run Code Online (Sandbox Code Playgroud)

但范围也不适用于列表......

我想这样做:

for b_idx, price_break in enumerate(part.standard_pricing):
Run Code Online (Sandbox Code Playgroud)

只是从末尾开始part.standard_pricing,让索引减少而不是增加

关于如何以Python方式做到这一点有什么想法吗?

python reverse loops

8
推荐指数
2
解决办法
9616
查看次数

为什么我收到错误 -“re.error:在位置 2 处多次重复”

我第一次学习 re 模块但遇到了错误。

代码-

import re
my_str='''pyhton
c++
java
c++
js node
ds algo
pyhton
js node
javac++
java
js node
ds algo'''
var = re.findall("c++",my_str)
Run Code Online (Sandbox Code Playgroud)

它给出了错误 -re.error: multiple repeat at position 2

python python-re

3
推荐指数
1
解决办法
7420
查看次数

为什么以下代码给出错误 - “错误:'NoneType'对象不可调用”?

我试图在 python 中实现装饰器,但在第 14 行出现错误,即 hello()

    #The code-
    def maint(item1):
        def greet():
            print("Good Morning") 
            item1()
            print("Tanish")
        return greet()

    #decorator----
    @maint 
    def hello():
        print("Hello")
    # hello=maint(hello)
    hello()
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

python python-decorators

-1
推荐指数
1
解决办法
60
查看次数

标签 统计

python ×3

loops ×1

python-decorators ×1

python-re ×1

reverse ×1