【问题标题】:Count of characters in a string excluding special characters字符串中不包括特殊字符的字符数
【发布时间】:2017-09-23 04:44:41
【问题描述】:

我需要计算给定文件中的字符数。问题是,我没有正确拆分文件。如果我的输入文件有内容“The!dog-ate #####the,cat”,我不需要输出中的特殊字符。 o/p: t:4 h:2 e:3 !:1 d:1 o:1 g:1 -:1 #:5.... 另外,我需要删除“-”符号并确保这个词没有连接。

    from collections import Counter
    import sys
    filename = sys.argv[1]
    reg = '[^a-zA-Z+]'
    f = open(filename, 'r')
    x = f.read().strip()
    lines=[]
    for line in x:
       line = line.strip().upper()
       if line:
           lines.append(line)
    print(Counter(lines))

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: python regex counter


    【解决方案1】:

    使用re.sub 并删除特殊字符。

    import re
    
    with open(filename) as f:
        content = re.sub('[^a-zA-Z]', '', f.read(), flags=re.M)    
    counts = Counter(content)
    

    演示:

    In [1]: re.sub('[^a-zA-Z]', '', "The! dog-ate #####the,cat")
    Out[1]: 'Thedogatethecat'
    
    In [2]: Counter(_)
    Out[2]: 
    Counter({'T': 1,
             'a': 2,
             'c': 1,
             'd': 1,
             'e': 3,
             'g': 1,
             'h': 2,
             'o': 1,
             't': 3})
    

    注意,如果你想同时计算大写和小写,你可以将content转换为小写:

    counts = Counter(content.lower())
    

    【讨论】:

      【解决方案2】:

      只需删除不需要的值:

      c = Counter(lines)
      del c['#']
      del c['-']
      del c[',']
      print(c)
      

      【讨论】:

        【解决方案3】:

        foo.txt

        asdas
        
        !@#!@
        
        
        asdljh
        
        
        12j3l1k23j
        

        发件人:

        https://docs.python.org/3/library/string.html#string.ascii_letters

        import string
        from collections import Counter
        
        with open('foo.txt') as f:
            text = f.read()
        
        filtered_text = [char for char in text if char in in string.ascii_letters]
        counted = Counter(filtered_text)
        print(counted.most_common())
        

        输出

        [('a', 3), ('j', 3), ('s', 3), ('d', 2), ('l', 2), ('h', 1), ('k', 1)]
        

        【讨论】:

        • 事先将ascii_letters 转换为一组将提高效率。对字符串的查找是线性时间。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-25
        • 2019-11-20
        • 2014-05-31
        • 1970-01-01
        • 2017-08-22
        • 2020-07-12
        相关资源
        最近更新 更多