简体   繁体   English

如何计算python中的后缀?

[英]How to count suffixes in python?

I was working on a couple of things and first off I had to split a string (it was a paragaph) and then take off the punctuation of each string. 我正在做一些事情,首先我必须分割一个字符串(这是一个paragph),然后取下每个字符串的标点符号。 For that I did the following: 为此,我执行了以下操作:

a = string1.split()

print(a)

punc = "?.,!"

a = ["".join(c for c in s if c not in punc) for s in a]

print(a)

After this I have to count how many words end in "ing". 在此之后,我必须计算以“ ing”结尾的单词数。 I tried various ways but for some reason they always count 0 when they should count 10. This was my latest attempt: 我尝试了各种方法,但由于某种原因,它们总是在应计数为10时计数为0。这是我最近的尝试:

suffix = "ing"

suffix_count = 0

if any (''.endswith(suffix) for s in a):

    suffix_count += 1

print ("Found {} words ending in ing".format(suffix_count))

What am I doing wrong? 我究竟做错了什么?

Instead of 代替

if any (''.endswith(suffix) for s in a):
    suffix_count += 1

try 尝试

suffix_count = sum(1 for s in a if s.endswith(suffix))

The issue with your code is that you're checking whether the empty string ends with 'suffix' (which is always false) but even if you got that check right: you're increasing suffix_count just once unrelated to how many strings end in 'suffix'. 您的代码存在的问题是,您正在检查空字符串是否以“后缀”结尾(始终为假),但是即使您没做错检查:您只是一次增加了suffix_count,与多少个以“'”结尾的字符串无关后缀'。

You could do: 您可以这样做:

suffix_count = len([s for s in a if s.endswith(suffix)])

Simply forms the list of all strings that end with the suffix, then computes the length of that list, which is of course the number of strings that ended with the suffix. 只需形成所有以后缀结尾的字符串的列表,然后计算该列表的长度即可,当然,这就是以后缀结尾的字符串的数量。 I think it's cleaner than an explicit for loop. 我认为这比显式的for循环更干净。

There is a much easier way to remove punc and sum using rstrip so we don't need to loop over every char: 有一种使用rstrip删除punc和sum的简便方法,因此我们不需要遍历每个char:

sum(s.rstrip(punc).endswith("ing") for s in a)

So your double loop can be replaced with the following to remove the punc: 因此,您的double循环可以替换为以下内容以删除punc:

a = [s.rstrip(punc) for s in a]

Also using string.punctuation may be better: 同样使用string.punctuation可能更好:

from string import punctuation
a = [s.rstrip(punctuation) for s in a]

try this out: 试试看:

a = ["singing", "dancing", "playing"]

suffix = "ing"

suffix_count = 0

for s in a:
    if s.endswith(suffix):
        suffix_count += 1

print ("Found {} words ending in ing".format(suffix_count))

For me it printed 3. 对我来说它印了3。

Hope it helps 希望能帮助到你

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

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