简体   繁体   中英

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)how to search NR0951113 whether in the PageNumList or not?

EDITED

2)if i want to get the NR0951113 array index?how to get it?

I think you probably want:

any(x.DeviceID == 'NR0851113' for x in PageNumList)

If you actually want to get the index, then possibly next is the builtin you should be using:

next(i for i,x in enumerate(PageNumList) if x.DeviceID == 'NR085113')

This will raise StopIteration if the DeviceID isn't found on any of your objects. You can prevent the StopIteration by passing a second value to next which is returned if the iterable you pass in is empty:

index = next((i for i,x in enumerate(PageNumList) if x.DeviceID == 'NR085113'),None)
if index is not None:
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM