在Python 2.7.x中,检查嵌套元组中是否存在字符串(或任何其他数据类型)的最佳(也是最快)方法是什么?
例如:
RECIPES = (
('apple', 'sugar', 'extreme_Force'),
('banana', 'syrup', 'magical_ends'),
('caramel', 'chocolate', 'pancake_MONSTER'),
('banana',('someAnother','banana'))
)
如果banana
出现在任何嵌套元组中并返回位置索引,则需要检查此元组,在本例中为1,0
。
此外,元组可以嵌套到任何深度。
答案 0 :(得分: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)]
答案 1 :(得分: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'
答案 2 :(得分: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
答案 3 :(得分: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)]
这适用于您的示例,因为数据排序很好。我想应该注意的是,这对于你提出的任意嵌套都不起作用(但我会留在这里以防其他人发现这个问题有类似的,更有限的问题)。