[英]How to count the suffixes in words contained in a csv file?
I have previously found a way to count the prefixes, as shown below, so is there a way similar to this which is so obvious I'm missing it completely? 我以前已经找到了一种计算前缀的方法,如下所示,所以有没有一种类似于此方法的方法,以至于我完全忽略了它?
for i in range (0, len(hardprefix)):
if len(word) > len(hardprefix[i]):
if word.startswith(hardprefix[i]):
hardprefixcount += 1
break
I need this code to use the first column of the file and count the number of a set array of suffixes found within these words 我需要此代码来使用文件的第一列,并计算在这些单词中找到的后缀集合的数量
This is what i have so far 这就是我到目前为止
for i in range (0, len(easysuffix)):
if len (word) > len(easysuffix[i]):
if word.endswith(easysuffix[i]):
easysuffixcount += 1
break
below is a sample of my data from the csv file, with the arrays using the suffixes below that 以下是来自csv文件的数据示例,其中的数组使用的是后缀
on 1
only 4
our 1
own 1
part 7
piece 4
pieces 4
place 1
pressed 1
riot 1
september 1
shape 3
hardsuffix = ['ism']
easysuffix = ['ity', 'esome', 'ece']
Your input file is tab delimited CSV so you can use the csv
module to process it. 您的输入文件是制表符分隔的CSV,因此您可以使用
csv
模块进行处理。
import csv
suffixes = ['ity', 'esome', 'ece']
with open('input.csv') as words:
suffix_count = 0
reader = csv.reader(words, delimiter='\t')
for word, _ in reader:
if any(word.endswith(suffix) for suffix in suffixes):
suffix_count += 1
print "Found {} suffix(es)".format(suffix_count)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.