当前位置: 动力学知识库 > 问答 > 编程问答 >

Python counting string

问题描述:

I have to define a function called letterCount in which it returns an integer for the number of times there is a string in the function. For some reason, it returns 2 and I am supposed to return 7

def letterCount(text,collection):

for collection in text:

if collection in text:

return len(collection) + 1

seuss = 'The cat in a hat came back'

letters = 'ac'

print(letterCount(seuss,letters))

网友答案:

Maybe you want this:

def letterCount(text,collection):
    res = 0
    for c in text:
        if c in collection:
            res = res + 1
    return res
seuss = 'The cat in a hat came back'
letters = 'ac'
print(letterCount(seuss,letters))

you define an variable collection in for collection in text:, it hidden the variable in def letterCount(text,collection).

网友答案:

I think the syntax of the python code you wrote is wrong. this is most probably what you want

def letterCount(text,collection):
  for collection in text:
    if collection in text:
        return len(collection) + 1
seuss = 'The cat in a hat came back'
Letters = 'ac'
print(letterCount(seuss,Letters))

your question is also not clear. can you elaborate a little. if what you want is to count the number of words in the string this would suffice

print len(seuss.split(' '))
网友答案:

If you want to count the number of times the string letters occurs in seuss:

def letterCount(text,collection):
    count = 0

    for i in range(0, len(text) - len(collection)):
        if collection == text[i:i + len(collection)]:
            count += 1

    return count

seuss = 'The cat in a hat came back'
letters = 'ac'
print(letterCount(seuss,letters))

If you want to count the number of times each letter in letters occurs in seuss:

def letterCount(text,collection):
    count = 0

    for i in range(0, len(text)):
        for c in collection:       
            if c == text[i]:
                count += 1

    return count

seuss = 'The cat in a hat came back'
letters = 'ac'
print(letterCount(seuss,letters))
网友答案:

Assuming you want to count the number of letters in the sentence, I'd do this:

def letterCount(text,collection):
  lettercount = 0
  for letter in collection:
    lettercount += text.count(letter)
  return lettercount


seuss = 'The cat in a hat came back'
Letters = 'ac'
print(letterCount(seuss,Letters)) # Returns 8

Explanation:

lettercount = 0 sets our counter to 0

for letter in collection: creates a loop through the list of the letters you passed to the function. In this case "a" and "c"

lettercount += text.count(letter) counts the number of times each letter appears in the sentence and adds it to the counter.

return lettercount sends the result back.

网友答案:

If you want to find out the number of times either 'a' or 'c' comes in 'The cat in a hat came back', then:

import collections
def letter_count(text, letters):
    counter = collections.Counter(text)
    return sum(counter.get(letter, 0) for letter in letters)
网友答案:

Try this:

import re
def letterCount(text,collection):
    return sum(len(re.findall(c, text)) for c in collection)

# re.findall(c, text) -> find all occurences of letter c in text
# len(...)            -> get the length of the list of all occurrences of c in text
# sum(...)            -> sum up all the partial results
分享给朋友:
您可能感兴趣的文章:
随机阅读: