【问题标题】:collection Counter - How to eliminate counting characters " ' " in a wordcollection Counter - 如何消除单词中的计数字符“'”
【发布时间】:2020-11-06 16:26:36
【问题描述】:

我编写了一个脚本来查找文件/字典中最长的单词。 但是在英语中使用撇号“'”,我想跳过它。

from collections import Counter
import time

words = open('english').read().splitlines()

time_before = time.time()

k = Counter(words)

longest = max(k, key=len)
print('The longest word in Dictionary is:', longest, 'has' , len(longest), 'characters')

time_after = time.time()
time_taken = time_after - time_before
print ( 'Longest word found in: ' , time_taken)
print(".......................")

【问题讨论】:

    标签: python python-3.x counter


    【解决方案1】:

    所以对于初学者来说,这里没有理由使用Counter,你实际上并没有计算任何东西(除非由于某种原因你的字典文件有重复的单词)

    您应该能够通过检查它们是否包含撇号的理解来过滤掉单词:

    import time
    
    with open('english') as f:
        words = f.read().splitlines()
    
    time_before = time.time()
    
    filtered = (word for word in words if "'" not in word)
    longest = max(filtered, key=len)
    print('The longest word in Dictionary is:', longest, 'has' , len(longest), 'characters')
    
    time_after = time.time()
    time_taken = time_after - time_before
    print ( 'Longest word found in: ' , time_taken)
    print(".......................")
    

    【讨论】:

      【解决方案2】:

      替换您用于max() 的密钥:

      longest = max(k, key=lambda s:len(letter for letter in s if letter != "'"))
      

      您当然可以自定义此解决方案以排除更多字母,或仅包含特定内容等。

      【讨论】:

        【解决方案3】:

        在单词长度计数中排除撇号及其后面的所有字符:

        max(k, key=lambda word: len(word.split("'")[0]))
        

        【讨论】:

          【解决方案4】:

          您可以将单引号替换为 none 然后开始计数。

          txt = "hi it's me"
          x = txt.replace("'", "")
          print(x)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-07-03
            • 1970-01-01
            • 2020-02-29
            • 1970-01-01
            • 1970-01-01
            • 2012-12-10
            相关资源
            最近更新 更多