繁体   English   中英

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

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

我正在尝试创建一个函数 - 给定一个字符串 - 将返回不允许的字符数('error_char'),如下所示:'不允许的总数/字符串的总长度'。

到目前为止,我已经尝试过:

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

但我得到的只是'0/56',我期待'22/56',因为m,x,y,z是'不允许'并且m重复19次

allowed_characters('aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz')
'0/56'

然后我试过了:

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))

但我得到相同的结果......

我也尝试过这些其他方法,但还是一样:

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

和...

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

我什至尝试更改逻辑并迭代“正确/错误字符”,但似乎没有任何效果......我一遍又一遍地得到相同的结果。 看起来好像循环在第一个字符之后立即停止,或者没有运行“elif”部分?

我会在这里使用len()使用正则表达式替换技巧:

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

以上返回输入字符串的长度减去输入的长度,所有允许的字符都被删除(或者减去字符串的长度,只有不允许的字符)。

每当涉及到更快的计数- 考虑Counter总是好的 你可以尝试像这样简化你的代码:

注意- 请不要在人们回复帖子的过程中更改您的问题描述。 这使得保持同步变得非常困难。

不过仍有改进的余地。

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)

或者,您真的想使用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