【发布时间】:2020-01-14 20:02:27
【问题描述】:
所以我现在正在学习 python,我真的需要你的帮助。 例如,您确实有随机文本文件,其中包含单词和数字。 你需要在这个文件中找到最长的单词和最大的数字。 上半场我搞定了,我找到了最长的一个字:
def longest_word(word):
with open(word, 'r') as infile:
words = infile.read().split()
max_len = len(max(words, key=len))
return [word for word in words if len(word) == max_len]
print (("the longest word is :"), longest_word ('text.txt'))
你能帮我完成第二部分吗?如何找到文件中的最大数量?
【问题讨论】:
-
我建议使用正则表达式将单词与数字分开,然后找到两者的最大值。
-
这些数字是非负整数、整数还是浮点数?
-
它们是整数
标签: python python-3.x file