4

在 Python 2.7.x 中,检查嵌套元组中是否存在字符串(或任何其他数据类型)的最佳(也是最快)方法是什么?

例如:

RECIPES = (
    ('apple', 'sugar', 'extreme_Force'),
    ('banana', 'syrup', 'magical_ends'),
    ('caramel', 'chocolate', 'pancake_MONSTER'),
    ('banana',('someAnother','banana'))
)

需要检查此元组是否banana出现在任何嵌套元组中并返回位置索引,在这种情况下1,0

此外,元组可以嵌套到任何深度。

4

4 回答 4

7

递归多位置索引:

import sys
from collections import Sequence,defaultdict

#making code python3-compatible
if sys.version_info[0] == 3:
    basestring = str

def buildLocator(tree):
    locator = defaultdict(list)
    def fillLocator(tree, locator,location):
        for index,item in enumerate(tree):            
            if isinstance(item,basestring):
                locator[item].append(location+(index,))
            elif isinstance(item,Sequence):
                fillLocator(item,locator, location+(index,))
    fillLocator(tree,locator,())
    return locator

RECIPES = (
    ('apple', 'sugar', 'extreme_Force'),
    ('banana', 'syrup', 'magical_ends'),
    ('caramel', 'chocolate', 'pancake_MONSTER'),
    ('banana',('someAnother','banana'))
)
locator = buildLocator(RECIPES)

print(locator['banana'])

印刷

[(1, 0), (3, 0), (3, 1, 1)]
于 2012-09-18T19:18:07.287 回答
4

如果您只需要第一个匹配项,则生成器可以很好地做到这一点:

def find_location(text):
    try:
        return next((i, j) 
            for i, t in enumerate(RECIPES)
            for j, v in enumerate(t)
            if v == text)
    except StopIteration:
        return (None, None)  # not found

用法:

>>> find_location('banana')
(1, 0)
>>> find_location('apple')
(0, 0)
>>> find_location('chocolate')
(2, 1)
>>> find_location('spam')
(None, None)

注意第一个值是整个RECIPES序列的索引,第二个是单个元组的索引;RECIPES[1][0] == 'banana'

于 2012-09-18T19:07:59.377 回答
1

使用 for 循环查找该项目是否存在,并在找到后立即中断循环。

In [48]: RECIPES = (
   ....:     ('apple', 'sugar', 'extreme_Force'),
   ....:     ('banana', 'syrup', 'magical_ends'),
   ....:     ('caramel', 'chocolate', 'pancake_MONSTER'),
   ....: )

In [49]: for i,x in enumerate(RECIPES):
   ....:     if 'banana' in x:
   ....:         print i,x.index('banana')
   ....:         break
   ....:         
   ....:         
1 0
于 2012-09-18T19:10:11.297 回答
1

为什么不试试numpy

import numpy as np
RECIPES = (
    ('apple', 'sugar', 'extreme_Force'),
    ('banana', 'syrup', 'magical_ends'),
    ('caramel', 'chocolate', 'pancake_MONSTER'),
)
np_recipes = np.array(recipes)
indices = zip(*np.where( np_recipes == 'banana' ) ) #[(1, 0)]

这适用于您的示例,因为数据排序良好。我想应该注意的是,这不适用于您所要求的任意嵌套(但我会留在这里,以防其他人发现这个问题有类似的、更受限制的问题)。

于 2012-09-18T19:16:30.923 回答