[英]Efficiently finding anagrams in two lists
I have two lists named 'query' and 'data', both of which contain strings.我有两个名为“查询”和“数据”的列表,它们都包含字符串。 I need to count how many anagrams of each string in 'query' there are in 'data'.
我需要计算“数据”中有多少“查询”中每个字符串的字谜。
For example for the following two lists:例如以下两个列表:
query = ['no', 'result', 'oh', 'abc', 'temper']查询= ['否','结果','哦','abc','脾气']
data = ['no', 'on', 'bca', 'oh', 'cba', 'repmet', 'serult', 'pemter', 'tluser', 'tlures', 'pterem', 'temrep']数据= ['no','on','bca','oh','cba','repmet','serult','pemter','tluser','tlures','pterem','temrep' ]
the output would be a dict with the anagram counts for each word: output 将是一个字典,其中包含每个单词的字谜计数:
{'no': 2, 'result': 3, 'oh': 1, 'abc': 2, 'temper': 4} {'no': 2, 'result': 3, 'oh': 1, 'abc': 2, 'temper': 4}
I have an initial brute force solution using nested loops but was wondering how I should go about optimizing this since it is pretty slow when the lists get larger.我有一个使用嵌套循环的初始蛮力解决方案,但想知道我应该如何 go 来优化它,因为当列表变大时它会很慢。
dict1 = {}
data.sort()
data.sort(key=len, reverse=False)
for idx in range(len(query)):
dict1[query[idx]] = 0
x = sorted(query[idx])
for idx2 in range(len(data)):
if len(data[idx2]) > len(query[idx]):
break
if data[idx2] == query[idx]:
dict1[query[idx]] += 1
elif x == sorted(data[idx2]):
dict1[query[idx]] += 1
You could use a Counter object:您可以使用计数器object:
from collections import Counter
query = ['no', 'result', 'oh', 'abc', 'temper']
data = ['no', 'on', 'bca', 'oh', 'cba', 'repmet', 'serult', 'pemter', 'tluser', 'tlures', 'pterem', 'temrep']
counts = Counter(''.join(sorted(word)) for word in data)
anagram_counts = {k:counts[''.join(sorted(k))] for k in query}
print(anagram_counts) #prints {'no': 2, 'result': 3, 'oh': 1, 'abc': 2, 'temper': 4}
This has linear complexity whereas your nested loops approach has quadratic complexity.这具有线性复杂性,而您的嵌套循环方法具有二次复杂性。 Even without using a Counter object, you can get linear complexity: one pass over
data
to create a dictionary of counts and a subsequent pass over query
, using the dictionary constructed in the first loop to create the target dictionary.即使不使用计数器 object,您也可以获得线性复杂度:一次传递
data
以创建计数字典和随后传递query
,使用在第一个循环中构造的字典来创建目标字典。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.