[英]Python Search The String in a Array List
StructPageNum = namedtuple('FDResult', ['DeviceID', 'PageNum'])
PageNumList = []
Node = StructPageNum(DeviceID='NR0951113', PageNum=[1,2,3,4])
PageNumList.append(Node)
Node = StructPageNum(DeviceID='NR0951114', PageNum=[1,2,3,4])
PageNumList.append(Node)
print('NR0951113' in PageNumList[:].DeviceID)
1)如何在PageNumList中搜索NR0951113?
2)如果我想獲取NR0951113數組索引?如何獲取?
我想您可能想要:
any(x.DeviceID == 'NR0851113' for x in PageNumList)
如果你真的想要得到的指數,則有可能next
就是你應該使用內置:
next(i for i,x in enumerate(PageNumList) if x.DeviceID == 'NR085113')
如果在您的任何對象上都找不到DeviceID,這將引發StopIteration
。 您可以通過將第二個值傳遞給next
來防止StopIteration
,如果傳入的可迭代項為空,則返回第二個值:
index = next((i for i,x in enumerate(PageNumList) if x.DeviceID == 'NR085113'),None)
if index is not None:
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.