简体   繁体   English

使用python计算字符串中多个字符的出现次数

[英]Counting occurrences of multiple characters in a string, with python

I'm trying to create a function that -given a string- will return the count of non-allowed characters ('error_char'), like so: 'total count of not-allowed / total length of string'.我正在尝试创建一个函数 - 给定一个字符串 - 将返回不允许的字符数('error_char'),如下所示:'不允许的总数/字符串的总长度'。

So far I've tried:到目前为止,我已经尝试过:

def allowed_characters(s):
    s = s.lower()
    correct_char = 'abcdef'
    error_char = 'ghijklmnopqrstuvwxyz'
    counter = 0
    
    for i in s:
        if i in correct_char:
            no_error = '0'+'/'+ str(len(s))
            return no_error
    
        elif i in error_char: 
            counter += 1
            result = str(sum(counter)) + '/' + str(len(s))
            return result

but all I get is '0/56' where I'm expecting '22/56' since m,x,y,z are 'not allowed' and m repeats 19 times但我得到的只是'0/56',我期待'22/56',因为m,x,y,z是'不允许'并且m重复19次

allowed_characters('aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz')
'0/56'

Then I've tried:然后我试过了:

def allowed_characters(s):
    s = s.lower()
    correct_char = 'abcdef'
    error_char = 'ghijklmnopqrstuvwxyz'
    counter = 0
    
    for i in s:
        if i in correct_char:
            no_error = '0'+'/'+ str(len(s))
            return no_error
    
        elif i in error_char: 
            import regex as re
            rgx_pattern = re.compile([error_char])
            count_e = rgx_pattern.findall(error_char, s)
            p_error = sum([count_e.count(i) for i in error_char])
            result = str(p_error) + '/' + str(len(s))

But I get the same result...但我得到相同的结果......

I've also tried these other ways, but keep getting the same:我也尝试过这些其他方法,但还是一样:

def allowed_characters1(s):
    s = s.lower()
    correct_char = 'abcdef'
    
    for i in s:
        if i not in correct_char:
            counter = sum([s.count(i) for i in s])
            p_error = str(counter) + '/' + str(len(s))
            return p_error
            
        elif i in correct_char:
            no_error = '0'+'/'+ str(len(s))
            return no_error

and...和...

def allowed_characters2(s):
    s = s.lower()
    correct_char = 'abcdef'
    
    for i in s:
        if i not in correct_char:
            counter = sum(s.count(i))
            p_error = str(counter) + '/' + str(len(s))
            return p_error
            
        elif i in correct_char:
            no_error = '0'+'/'+ str(len(s))
            return no_error

I've even tried changing the logic and iterating over 'correct/error_char' instead, but nothing seems to work... I keep getting the same result over and over.我什至尝试更改逻辑并迭代“正确/错误字符”,但似乎没有任何效果......我一遍又一遍地得到相同的结果。 It looks as though the loop stops right after first character or doesn't run the 'elif' part?看起来好像循环在第一个字符之后立即停止,或者没有运行“elif”部分?

I would use a regex replacement trick here using len() :我会在这里使用len()使用正则表达式替换技巧:

def allowed_characters(s):
    return len(s) - len(re.sub(r'[^ghijklmnopqrstuvwxyz]+', '', s))

The above returns the length of the input string minus the length of the input with all allowed characters removed (alternatively minus the length of the string with only non allowed characters).以上返回输入字符串的长度减去输入的长度,所有允许的字符都被删除(或者减去字符串的长度,只有不允许的字符)。

Whenever it comes to do quicker counting - it's always good to think about Counter You can try to simplify your code like this:每当涉及到更快的计数- 考虑Counter总是好的 你可以尝试像这样简化你的代码:

Notes - please don't change your Problem Description during the middle of people's answering posts.注意- 请不要在人们回复帖子的过程中更改您的问题描述。 That make it very hard to keep in-sync.这使得保持同步变得非常困难。

There is still room to improve it though.不过仍有改进的余地。

from collections import Counter

def allowed_char(s):
    s = s.lower()
    correct_char = 'abcdef'
    error_char = 'ghijklmnopqrstuvwxyz'
    
  
    ok_counts = Counter(s)
    print(f' allowed: {ok_counts} ')
    correct_count  = sum(count for c, count in ok_counts.items() if c in correct_char)

    error_count = sum(count for c, count in ok_counts.items() if c in error_char)
   
    #return sum(not_ok.values()) / total

    return correct_count, error_count  # print both 
    

s =('aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz')

print(allowed_char(s))             # (34, 22)

print(allowed_char('abdmmmmxyz'))  # (3, 7)

Alternatively, you really want to use for-loop and learn to process the string of characters, you could try this:或者,您真的想使用for-loop并学习处理字符串,您可以试试这个:


def loop_count(s):
    s = s.lower()
    correct_count = error_count = 0

    for c in s:
        if c in correct_char:
            correct_count += 1
        else:
            error_count += 1

    return correct_count, error_count

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

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