我编写了一个函数,该函数将字符串作为输入并移交表示最常出现的元素/字符的字典。
def mostoccur(arr):
n = len(arr)
dict={}
# filling the dictionary with itesm and their frequency using dict.get()
for i in range(n):
dict[arr[i]] = dict.get(arr[i],0) + 1
return dict
string = "aabbccdddddddd"
# turning the string into a list, so it's iterable
ls = list(string)
ans = mostoccur(ls)
print("The dictionary of all the characters and their frequency: \n",ans)
maximum = max(ans)
print("\nThe most occuring character is:",maximum)
但是后来我变得更加好奇,我想打印字典中出现次数最多和倒数第二的元素。因此,我写了这样的东西:
# defining a dictionary to store the most and second most occurring element
biggest = {'most-occuring':0, 'second-most-occuring':0}
# nested loops to go through all values in the dictionary
for a in ans.items(): # O(N)
for b in ans.items(): # O(N)
if a < b:
a,b = b,a
biggest['most-occuring'] = a
biggest['second-most-occuring']= b
# Total = O(N^2)
print(biggest)
我已经写了每个操作旁边的Big O,当我看着它时,我真的不喜欢我写的东西。我的意思是,O(N ^ 2)听起来太昂贵且效率低下。
您愿意以更好的方式阐明我的观点吗?
请记住,我不是在寻找一种利用 任何图书馆。
答案 0 :(得分:1)
这是在UserPaymentAccountHistoryLog
中执行的简单算法:
UserPaymentHistoryLog
这将打印O(n)
请注意,我在string = "aabbbccdddddddd"
def get_frequency(string):
chars = {}
for char in string:
chars[char] = chars.get(char, 0) + 1
biggest = [None, 0]
second = [None, 0]
for entry in chars.items():
char, count = entry
if count > biggest[1]:
second = biggest
biggest = entry
elif count > second[1]:
second = entry
return {
"most-occurring": biggest[0],
"second-most-occurring": second[0]
}
print(get_frequency(string))
上添加了一个额外的'b',以使其成为第二频繁出现的字母
答案 1 :(得分:0)
使用heapq.nlargest,如下所示:
from collections import Counter
import heapq
string = "aabbccdddddddd"
counts = Counter(string)
heapq.nlargest(2, counts, key=lambda k: counts[k])
并且不使用任何库,假设您的函数返回的内容与Counter:
keys = list(counts.keys())
keys.sort(key=lambda x: counts[x],reverse=True)
top_two = keys[:2] # just the keys
{ k : counts[k] for k in keys[:2] } # dict of top two