简体   繁体   English

查找字长

[英]Finding Word Length

I am looking for a script that can open myfile.txt, read it and count the number of words that have 10 or more letters in the file.我正在寻找一个可以打开 myfile.txt 的脚本,阅读它并计算文件中包含 10 个或更多字母的单词数。 I made several attempts and consistently receive syntax errors.我进行了多次尝试并始终收到语法错误。 Need help.需要帮忙。

Try like this:像这样尝试:

f = open("myfile.txt")
data = f.read()
wc = 0
for x in data.split():
  if len(x) >= 10:
    wc += 1

print(wc)

This is a possible solution:这是一个可能的解决方案:

with open('myfile.txt', 'r') as input_file:
    word_list = input_file.read().split()
    print(len(list(filter(lambda x: len(x)>10, word_list))))

There are a lot of different solutions though.虽然有很多不同的解决方案。 What did you try?你尝试了什么?

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

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