简体   繁体   English

计算字符串中出现的次数

[英]Counting number of occurrences in a string

I need to return a dictionary that counts the number of times each letter in a predetermined list occurs.我需要返回一个字典,计算预定列表中每个字母出现的次数。 The problem is that I need to count both Upper and Lower case letters as the same, so I can't use.lower or.upper.问题是我需要将大写和小写字母都计算为相同,所以我不能使用.lower或.upper。

So, for example, "This is a Python string" should return {'t':3} if "t" is the letter being searched for.因此,例如,如果“t”是要搜索的字母,“这是一个 Python 字符串”应该返回 {'t':3}。

Here is what I have so far...这是我到目前为止...

def countLetters(fullText, letters):
    countDict = {i:0 for i in letters}
    lowerString = fullText.lower()
    for i in lowerString:
        if i in letters:
            countDict[i] += 1

    return countDict

Where 'letters' is the condition and fullText is the string I am searching.其中“字母”是条件,全文是我正在搜索的字符串。

The obvious issue here is that if the test is "T" rather than "t", my code won't return anything Sorry for any errors in my terminology, I am pretty new to this.这里明显的问题是,如果测试是“T”而不是“t”,我的代码将不会返回任何内容抱歉我的术语中有任何错误,我对此很陌生。 Any help would be much appreciated!任何帮助将非常感激!

To ignore capitalization, you need to input = input = input.lower () .Lists all characters of the input text using list operations.要忽略大小写,需要 input = input = input.lower () 。使用列表操作列出输入文本的所有字符。 It can also be used as a word counter if you scan the space character.如果您扫描空格字符,它也可以用作单词计数器。

input = "Batuq batuq BatuQ" # Reads all inputs up to the EOF character

input = input.replace('-',' ')#Replace (-, + .etc) expressions with a space character.
input = input.replace('.','')
input = input.replace(',','')
input = input.replace("`",'')
input = input.replace("'",'')

#input= input.split(' ') #if you use it, it will sort by the most repetitive words
dictionary = dict()
count = 0
for word in input:
    dictionary[word] = input.count(word)
    
print(dictionary)

#Writes the 5 most repetitive characters
for k in sorted(dictionary,key=dictionary.get,reverse=True)[:5]:
    print(k,dictionary[k])

You're looping over the wrong string.您正在循环错误的字符串。 You need to loop over lowerString , not fullString , so you ignore the case when counting.您需要遍历lowerString ,而不是fullString ,因此在计数时忽略大小写。

It's also more efficient to do if i in countDict than if i in letter . if i in countDictif i in letter中更有效。

def countLetters(fullText, letters):
    countDict = {i.lower():0 for i in letters}
    lowerString = fullText.lower()
    for i in lowerString:
        if i in countDict:
            countDict[i] += 1

    return countDict

What you can do is simply duplicate the dict with both upper and lowercase like so:你可以做的是简单地复制大写和小写的字典,如下所示:

def countLetters(fullText, letters):
    countDict = {}
    for i in letters:
        countDict[i.upper()]=0
        countDict[i.lower()]=0
    lowerString = fullText.lower()
    letters = letters.lower()
    for i in lowerString:
        if i in letters:
            countDict[i] += 1
            if (i!=i.upper()):
                countDict[i.upper()] +=1

    return countDict
print(countLetters("This is a Python string", "TxZY"))

Now some things you can also do is loop over the original string and change countDict[i] += 1 to countDict[i.lower()] +=1现在您还可以做的一些事情是遍历原始字符串并将countDict[i] += 1更改为countDict[i.lower()] +=1

Would something like this work that handles both case sensitive letter counts and non case sensitive counts?像这样的工作会同时处理区分大小写的字母计数和不区分大小写的计数吗?

from typing import List

def count_letters(
    input_str: str,
    letters: List[str],
    count_case_sensitive: bool=True
):
    """
    count_letters consumes a list of letters and an input string
    and returns a dictionary of counts by letter. 
    """
    if count_case_sensitive is False:
        input_str = input_str.lower()
        letters = list(set(map(lambda x: x.lower(), letters)))
    # dict comprehension - build your dict in one line
    # Tutorial on dict comprehensions: https://www.datacamp.com/community/tutorials/python-dictionary-comprehension
    counts = {letter: input_str.count(letter) for letter in letters}
 
    return counts

# define function inputs    
letters = ['t', 'a', 'z', 'T']
string = 'this is an example with sTrings and zebras and Zoos'

# case sensitive
count_letters(
    string,
    letters,
    count_case_sensitive=True
)
# {'t': 2, 'a': 5, 'z': 1, 'T': 1}

# not case sensitive
count_letters(
    string,
    letters,
    count_case_sensitive=False
)
# {'a': 5, 'z': 2, 't': 3} # notice input T is now just t in dictionary of counts

Try it - like this:试试看 - 像这样:

def count_letters(fullText, letters):
    countDict = {i: 0 for i in letters}
    lowerString = fullText.lower()
    for i in lowerString:
        if i in letters:
            countDict[i] += 1
    return countDict

test = "This is a Python string."
print(count_letters(test, 't')) #Output: 3

Use the Counter from the collections module使用 collections 模块中的计数器

from collections import Counter
input = "Batuq batuq BatuQ"
bow=input.split(' ')
results=Counter(bow)
print(results)

output: Counter({'Batuq': 1, 'batuq': 1, 'BatuQ': 1}) output:计数器({'Batuq':1,'batuq':1,'BatuQ':1})

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

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