[英]appending specific words to list from file in python
I am writing a program that reads from a file of 50,000 words and it needs to get the percentage of words that do not have the letter 'e' in them. 我正在编写一个程序,该程序从50,000个单词的文件中读取内容,并且需要获取其中没有字母'e'的单词的百分比。 I can get the program to print all the words without e's but I want to append them to a list so that I can get the sum of the elements within the list.
我可以使程序打印出所有不带e的单词,但我想将它们附加到列表中,以便获得列表中元素的总和。 What I have now gives me the result of 0 every time I run it.
现在,每次运行时,我得到的结果都为0。 It also produces the total amount of lines which is correct.
它还会产生正确的总行数。 Sorry, I am not the best in python.
抱歉,我不是python最好的。
f=open("hardwords.txt")
def has_no_e(f):
words = []
sum_words= len(words)
total = sum(1 for s in f)
print total
print sum_words
letter = 'e'
for line in f:
for l in letter:
if l in line:
break
else:
words.append(line)
has_no_e(f)
You don't need to collect the words, just count them. 您无需收集单词,只需数数即可。
Untested: 未经测试:
total = 0
without_e = 0
with open("hardwords.txt") as f:
for line in f:
total = total + 1
if not 'e' in line:
without_e = without_e + 1
percentage = float(without_e) / float(total)
What about this: 那这个呢:
def has_no_e():
with open(path, "r") as f:
words = [word.strip() for line in f.readlines() for word in line.strip().split(',')]
words_without_e = [word for word in words if 'e' not in word]
print len(words), words
print len(words_without_e), words_without_e
has_no_e()
Now you just need to calculate the percentage 现在您只需要计算百分比
This does just so: 这样做是这样的:
def has_no_e(path):
total_words = 0
words_without_e = 0
with open(path, "r") as f:
for line in f:
words = line.lower().split()
total_words += len(words)
words_without_e += sum("e" not in w for w in words)
return (float(words_without_e)/total_words)*100
This a possible way to do it: 这是一种可能的方法:
with open('G:\Tmp\demo.txt', 'r') as f:
total = 0
count = 0
for line in f:
words = line.split()
total = total + len(words)
count = count + len([w for w in words if w.find('e') > 0])
print 'Total word:{0}, counted:{1}'.format(total, count)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.