简体   繁体   English

有没有一种方法可以在不使用预定义函数/集合/字典的情况下计算列表中元素的频率?

[英]Is there a way to count the frequency of an element in a list without using predefined functions/sets/dictionaries?

first want to say I am new to Python but I am eager to learn and have searched around for a solution, can't seem to figure this problem out without resorting to many lines of code.首先想说我是 Python 的新手,但我渴望学习并四处寻找解决方案,如果不借助多行代码似乎无法解决这个问题。

We recently recieved an assignment for our course which looks this:我们最近收到了一份课程作业,看起来是这样的:

Write a program that, given a text, computes the frequency of every letter and outputs the most and the least frequent one.

Do not use: 
    •   external libraries (no "import" statement is allowed)
    •   dictionaries
    •   sets
    •   predefined functions (e.g., max(), min() )

I have asked for clarification on the predefined functions but have not received a reply.我已要求澄清预定义功能,但未收到回复。 Would be grateful for your input or feedback, I'll paste my code so far below, it's unfinished but gives an idea of what it will look like.非常感谢您的输入或反馈,我将在下面粘贴我的代码,它尚未完成但给出了它的外观的想法。 I did use "count" in it but I can replace that with my own counters if we aren't allowed to use "count."我确实在其中使用了“计数”,但如果我们不允许使用“计数”,我可以用我自己的计数器替换它。


txt = input('Skriv en text: ').replace(" ","").lower()

counters = []


for c in txt:
    counters.append(c)

a = counters.count("a")
b = counters.count("b")
c = counters.count("c")
d = counters.count("d")
e = counters.count("e")
f = counters.count("f")
g = counters.count("g")
h = counters.count("h")
i = counters.count("i")
j = counters.count("j")
k = counters.count("k")
l = counters.count("l")
m = counters.count("m")
n = counters.count("n")
o = counters.count("o")
p = counters.count("p")
q = counters.count("q")
r = counters.count("r")
s = counters.count("s")
t = counters.count("t")
u = counters.count("u")
v = counters.count("v")
x = counters.count("x")
y = counters.count("y")
z = counters.count("z")

if a >= b or c or d or e or f or g or h or i or j or k or l or m or n or o or p or q or r or s or t or u or v or x or y or z:
    print ("A has the highest frequency.")
elif b >= a or c or d or e or f or g or h or i or j or k or l or m or n or o or p or q or r or s or t or u or v or x or y or z:
    print()

    # Will do this for each letter and for the minimum value as well


For each character in the string, add it and its count to a list if it's not already in the list.对于字符串中的每个字符,如果它不在列表中,则将其及其计数添加到列表中。 This requires nested loops and therefore has terrible performance implications for large strings.这需要嵌套循环,因此对大字符串有可怕的性能影响。

def count(s):
  counts = []
  for c in s:
    if not any(True for (k, _) in counts if k == c):
      counts.append((c, s.count(c)))
  return counts  
>>> count("hello world")
[('h', 1), ('e', 1), ('l', 3), ('o', 2), (' ', 1), ('w', 1), ('r', 1), ('d', 1)]

I believe Chris solution is the smallest possible, I however prefer readability, so here it's an alternative solution:我相信 Chris 解决方案是尽可能小的,但我更喜欢可读性,所以这是一个替代解决方案:

def count_char(word):
    counter_list = []

    for char in word:        
        index = get_index(char, counter_list)
        if(index == -1):
            counter_list.append((char, 1))
        else:
            char, value = counter_list[index]
            value += 1
            counter_list[index] = (char, value)
    
    return counter_list



def get_index(character, haystack) -> int:

    for index, char_tuple in enumerate(haystack):
        char, _ = char_tuple
        if char == character:
            return index

    return -1

count_char("myrepeatedword")

As already stated, due to the constraints of your problem, the performance here is far away from being optimal.如前所述,由于您的问题的限制,此处的性能远非最佳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM