在python中搜索字节序列

时间:2017-10-25 09:09:52

标签: arrays python-2.7 search

我收到如下数组:

array('b', [71, 69, 84, 32, 47, 97, 115, 115, 101])

我想知道其中是否出现某个字节序列,例如47 98 113

最快捷,最pythonic的方法是什么?

1 个答案:

答案 0 :(得分:1)

首先将其转换为包含.tolist()的列表,然后如果要搜索确切序列,这可能会有所帮助:

a = [71, 69, 84, 32, 47, 97, 115, 115, 101]
b = [47, 98, 113]
def search(what, where):
    if len(what) > len(where):
        return False
    idx = [i for i, x in enumerate(where) if what[0] == x]
    for item in idx:
        if where[item:item+len(what)] == what:
            return True
    return False
search(b,a)
#False