当前位置: 动力学知识库 > 问答 > 编程问答 >

c - 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:
    ...
分享给朋友:
您可能感兴趣的文章:
随机阅读: