class Barcode:
def __init__(self, code, code_id = None):
self.code = code
self.id = code_id
我有一个条形码列表。我如何获得具有特定ID的条形码?如何从python列表中获取特定项目
class Barcode:
def __init__(self, code, code_id = None):
self.code = code
self.id = code_id
我有一个条形码列表。我如何获得具有特定ID的条形码?如何从python列表中获取特定项目
如果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?
PS:为什么不使用dict
?这是为了做这种工作而生的。
@Mitch我不认为这是一个*显而易见的*重复,因为列表没有必要排序。说到没有排序的情况,你能提供一个更好的方法吗?为了完整性,添加了残酷的方法。 –
@Mitch好吧,你的回答比较好,我真的觉得你应该在这里发表一个答案。 –
@Mitch你说得对。似乎低于这个问题的答案可以帮助这个OP很多。关闭投票。 –
列表是按ID排序的吗? –
另外,你会考虑使用id =>条码代码来存储代码而不是列表吗? –
每个id都是使用uuid.uuid4()生成的唯一的十六进制,我只是想从列表中获得它:select barcode where where barcode.id = something –