对应于列表中的一组元素的一组索引 python3
Set of indices corresponding to a set of elements in a list python3
我在列表中提到了 this post for finding the index(ices) corresponding to a single named element,但未能在答案 there/documentation.
中找到我的查询的答案
具体来说:是否有比仅迭代上面 link 中的方法更有效的方法来查找与一组元素对应的索引?
假设我有列表
mybiglist=['abc','def','ghi','jkl','mno']
并且我想要对应于 'abc','jkl'
的索引
我能做到:
mytargetlist=['abc','jkl']
for string in mytargetlist:
print(mybiglist.index('%s' %string))
但感觉应该有比循环更高效的方法?
如果上下文有所不同,我试图找到与图中某些顶点对应的索引,以便我可以使用 induce_subgraph 创建包含这些顶点的子图。不幸的是,我只知道我想要的名称标签,它们属于顶点,induce_subraph 的参数是:induce_subgraph(graph, vertex set)
你可以在你的大列表中理解一次。
mybiglist=['abc','def','ghi','jkl','mno']
mytargetlist=['abc','jkl']
[i for i, v in enumerate(mybiglist) if v in mytargetlist]
我认为你的方法有点简单,也许可以通过列表理解来简化更多(这只是语法糖...):
mybiglist = ['abc', 'def', 'ghi', 'jkl', 'mno']
mytargetlist = ['abc', 'jkl']
print([mybiglist.index(target) for target in mytargetlist])
如果你想要一个矫枉过正的解决方案,你可以使用 numpy
与 isin
和 where
来获取索引,而无需自己迭代它:
import numpy as np
mybiglist = np.array(['abc', 'def', 'ghi', 'jkl', 'mno'], dtype=str)
mytargetlist = ['abc', 'jkl']
print(*np.where(np.isin(mybiglist, mytargetlist)))
但这似乎有点荒谬:P
mybiglist.index(string)
具有复杂性 O(n)
,因此基本上您在 mybiglist
和 mytargetlist
上执行双重 for 循环。您可以通过 enumerate
:
进行改进
indices = {v:i for i,v in enumerate(mybiglist)}
您可以稍后访问索引,即 indices['abc']
。
这会将您的目标列表拆分成块并启动单独的线程。更多信息 here:
import concurrent.futures
mybiglist=['abc','def','ghi','jkl','mno']
mytargetlist=['abc','jkl']
def get_index(x):
return mybiglist.index(x)
with concurrent.futures.ProcessPoolExecutor() as executor:
results = executor.map(get_index, mytargetlist)
print(list(results))
我在列表中提到了 this post for finding the index(ices) corresponding to a single named element,但未能在答案 there/documentation.
中找到我的查询的答案具体来说:是否有比仅迭代上面 link 中的方法更有效的方法来查找与一组元素对应的索引?
假设我有列表
mybiglist=['abc','def','ghi','jkl','mno']
并且我想要对应于 'abc','jkl'
我能做到:
mytargetlist=['abc','jkl']
for string in mytargetlist:
print(mybiglist.index('%s' %string))
但感觉应该有比循环更高效的方法?
如果上下文有所不同,我试图找到与图中某些顶点对应的索引,以便我可以使用 induce_subgraph 创建包含这些顶点的子图。不幸的是,我只知道我想要的名称标签,它们属于顶点,induce_subraph 的参数是:induce_subgraph(graph, vertex set)
你可以在你的大列表中理解一次。
mybiglist=['abc','def','ghi','jkl','mno']
mytargetlist=['abc','jkl']
[i for i, v in enumerate(mybiglist) if v in mytargetlist]
我认为你的方法有点简单,也许可以通过列表理解来简化更多(这只是语法糖...):
mybiglist = ['abc', 'def', 'ghi', 'jkl', 'mno']
mytargetlist = ['abc', 'jkl']
print([mybiglist.index(target) for target in mytargetlist])
如果你想要一个矫枉过正的解决方案,你可以使用 numpy
与 isin
和 where
来获取索引,而无需自己迭代它:
import numpy as np
mybiglist = np.array(['abc', 'def', 'ghi', 'jkl', 'mno'], dtype=str)
mytargetlist = ['abc', 'jkl']
print(*np.where(np.isin(mybiglist, mytargetlist)))
但这似乎有点荒谬:P
mybiglist.index(string)
具有复杂性 O(n)
,因此基本上您在 mybiglist
和 mytargetlist
上执行双重 for 循环。您可以通过 enumerate
:
indices = {v:i for i,v in enumerate(mybiglist)}
您可以稍后访问索引,即 indices['abc']
。
这会将您的目标列表拆分成块并启动单独的线程。更多信息 here:
import concurrent.futures
mybiglist=['abc','def','ghi','jkl','mno']
mytargetlist=['abc','jkl']
def get_index(x):
return mybiglist.index(x)
with concurrent.futures.ProcessPoolExecutor() as executor:
results = executor.map(get_index, mytargetlist)
print(list(results))