2017-11-05 3 views
0

Der Code, den ich unten habe, sollte die Anzahl der Wörter, die mit einem bestimmten Buchstaben beginnen, laufen lassen, aber wenn ich ihn benutze, sind die Zählungen alle 0, anstatt was sie sein sollten: {'I' : 2, 'b': 2, 't': 3, 'f': 1}. Ich schätze jede Hilfe. Vielen Dank!Code läuft nicht richtig

def initialLets(keyStr): 
     '''Return a dictionary in which each key is the initial letter of a word in t and the value is the number of words that begin with that letter. Upper 
     and lower case letters should be considered different letters.''' 
     inLets = {} 
     strList = keyStr.split() 
     firstLets = [] 
     for words in strList: 
      if words[0] not in firstLets: 
       firstLets.append(words[0]) 
     for lets in firstLets: 
      inLets[lets] = strList.count(lets) 
     return inLets 

text = "I'm born to trouble I'm born to fate" 
print(initialLets(text)) 

Antwort

4

Sie können dies versuchen:

text = "I'm born to trouble I'm born to fate" 
new_text = text.split() 
final_counts = {i[0]:sum(b.startswith(i[0]) for b in new_text) for i in new_text} 

Ausgang:

{'I': 2, 'b': 2, 't': 3, 'f': 1} 
+1

Wörterbuch Verständnis ist richtig, was zu tun ist –

0

Sie haben noch einen Zähler, wie Sie den Brief anhängen, aber nicht die Zahl der Vorkommnisse.

So zu vereinfachen:

def initialLets(keyStr): 
     '''Return a dictionary in which each key is the initial letter of a word in t and the value is the number of words that begin with that letter. Upper 
     and lower case letters should be considered different letters.''' 

     strList = keyStr.split() 

     # We initiate the variable that gonna take our results 
     result = {} 

     for words in strList: 
      if words[0] not in result: 
       # if first letter not in result then we add it to result with counter = 1 
       result[words[0]] = 1 
      else: 
       # We increase the number of occurence 
       result[words[0]] += 1 

     return result 

text = "I'm born to trouble I'm born to fate" 
print(initialLets(text)) 
0

Zunächst werden überprüfen Sie, wenn Sie die ersten Buchstaben des Wortes in der Liste enthalten sind, bevor es bei der Umsetzung Das würde nur die Liste besteht aus nur 1 von jedem Buchstaben machen.. Zweitens ist Ihr strList eine Liste jedes Wortes, anstelle von inLets[lets] = strList.count(lets), sollte es inLets[lets] = firstLets.count(lets) sein ... Während Ihr gegenwärtiger Code nicht der sauberste Weg ist, es zu tun, hätte diese geringfügige Änderung gearbeitet.

def initialLets(keyStr): 
    '''Return a dictionary in which each key is the initial letter of a word in t and the value is the number of words that begin with that letter. Upper 
    and lower case letters should be considered different letters.''' 
    inLets = {} 
    strList = keyStr.split() 
    firstLets = [] 
    for words in strList: 
     firstLets.append(words[0]) 
    for lets in firstLets: 
     inLets[lets] = firstLets.count(lets) 
    return inLets 

text = "I'm born to trouble I'm born to fate" 
print(initialLets(text))