如何从python lis中获取特定项

2024-04-24 13:57:14 发布

您现在位置:Python中文网/ 问答频道 /正文

class Barcode:

    def __init__(self, code, code_id = None):
        self.code = code
        self.id = code_id  

我有一份条形码清单。如何获取具有特定id的条形码?在


Tags: selfnoneidinitdefcodebarcodeclass
1条回答
网友
1楼 · 发布于 2024-04-24 13:57:14

如果您的BarcodeList没有排序,我建议您使用野蛮的方法:

def find_item(id):
    for item in BarcodeList:
        if item.id == id:
            return item

如果已排序,请参见What is the best way to get the first item from an iterable matching a condition?

注:为什么不使用dict?它天生就是做这种工作的。在

相关问题 更多 >