Damit muss ich nicht nur die Häufigkeit der Buchstaben in einem Wort zählen, sondern auch die nicht verwendeten Buchstaben ausspucken.

alphabetList = ["a","b","c","d","e","f","g","h","i","j","k",\
    "l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]


def Checksums():
    for i in alphabetList:

        character = i
        myString = Phrase
        myList = []

        for i in myString:
            myList.append(i)

        myList = myList.sort()
        print myList

        count = 0
        for i in myList:
            if i == character:
                count = count +1
            else:
                continue
        print("There are ", count, " occurrences of ", character )


#input
Phrase = str(raw_input("Enter a Phrase: "))

Phrase = Phrase.lower()
# print (Phrase)
Phrase = Phrase.replace(" ", "")
# print Phrase

Checksums()

Ein Beispiel für eine Eingabe könnte sein:

aaA cC D <br>

Und die Rückkehr wäre

"There were '3' occurrences of the letter 'a'"<br>
"There were '2' occurrences of the letter 'c'"<br>
"Only 1 'd'"<br>
"The remaining letters are unused": b, e, etc...

Ich habe versucht, die Alphabetliste zu verwenden und diese einfach durchzugehen, erhalte jedoch die folgende Fehlermeldung:

TypeError: Das Objekt 'NoneType' ist nicht iterierbar

-1
PsychopathNextdoor 24 Feb. 2020 im 10:43

5 Antworten

Beste Antwort

Ersetzen Sie myList = myList.sort() in Ihrer Funktion durch myList.sort().

0
Alla Tarighati 24 Feb. 2020 im 07:54

Einige Möglichkeiten zur Vereinfachung Ihres Codes

Verwenden Sie zunächst ein Diktat, um die Anzahl der Vorkommen jedes Buchstabens in einer Zeichenfolge zu zählen:

d = {};
for c in myString:
     d[c] = (d[c]+1) if (c in d) else 1

Um dann die Statistiken jedes Buchstabens auszudrucken:

# used letters:
for k in d:
    print("There are ", d[k], " occurrences of ", k)


#unused letters
for o in range(ord('a'),ord('z')):
   k = chr(o)
   if not (k in d):
       print("No occurrence of ", k, " in string")

Oder kombiniert:

for o in range(ord('a'),ord('z')):
   k = chr(o)
   if not (k in d):
       print("No occurrence of ", k, " in string")
   else:
       print("There are ", d[k], " occurrences of ", k)
0
selbie 24 Feb. 2020 im 08:01

Verwenden Sie nicht alle 'i' als Variable, da es sich um eine verschachtelte Schleife handelt

-2
Jun Lim 24 Feb. 2020 im 07:52

Verwenden von Pythons Zähler und eines set, um die fehlenden Schlüssel zu ermitteln, führt zu:

import string
from collections import Counter


def check(phrase):
    phrase = phrase.lower().replace(" ", "")
    phrase = Counter(phrase)

    for counts in phrase.items():
        print ("There were %s occurrences of the letter %s" % counts[::-1])

    missingCharacter = set(string.ascii_lowercase) - set(phrase.keys())
    print("The remaining letters are unused: %s" % ','.join(missingCharacter))


check('aaA cC D')

Ausgabe:

There were 3 occurrences of the letter a
There were 2 occurrences of the letter c
There were 1 occurrences of the letter d
The remaining letters are unused: b,e,g,f,i,h,k,j,m,l,o,n,q,p,s,r,u,t,w,v,y,x,z
1
Maurice Meyer 24 Feb. 2020 im 07:57

Sie können dies versuchen

alphabetList = ["a","b","c","d","e","f","g","h","i","j","k",\
"l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
def occurence_counter(phrase):
    unused_letters = ""
    for alphabet in alphabetList:
        count = phrase.count(alphabet)
        if count > 1:
            print("There are ", count, " occurrences of ", alphabet )
        else:
            unused_letters = unused_letters + alphabet
    print("The remaining letters are unused" + unused_letters)
Phrase = str(input("Enter a Phrase: "))

Phrase = Phrase.lower()
occurence_counter(Phrase)
0
Henok Teklu 24 Feb. 2020 im 08:05