如何解决如何在python的文本文件中打印单词的行号?
我试图找到一种方法来从txt文件中打印最大的单词,它的大小和最上面的一行。我设法完成了另外两个,但是无法弄清楚如何打印行号。谁能帮我吗?
def LargestWord():
list_words = []
with open('song.txt','r') as infile:
lines = infile.read().split()
for i in lines:
words = i.split()
list_words.append(max(words,key=len))
largest_word = str(max(list_words,key=len))
print largest_word
print len(largest_words)
FindWord(largest_word)
def FindWord(largest_word):
解决方法
由于我正在处理的信息量有限,这是我能想到的最佳解决方案。假设每一行都由换行分隔,例如'\ n',则可以执行以下操作:
def FindWord(largest_word):
with open('song.txt','r') as infile:
lines = infile.read().splitlines()
linecounter = 1
for i in lines:
if largest_word in lines:
return linecounter
linecounter += 1
,
您可以使用枚举来获取当前行,并使用 lambda 进行排序以获取最长的单词:
def longest_word_from_file(filename):
list_words = []
with open(filename,'r') as input_file:
for index,line in enumerate(input_file):
words = line.split()
list_words.append((max(words,key=len),index))
sorted_words = sorted(list_words,key=lambda x: -len(x[0]))
longest_word,line_index = sorted_words[0]
return longest_word,line_index
,
您不需要遍历每行最大单词列表的其他循环。每个for循环都会增加功能时间和复杂性,因此最好避免出现不必要的情况。
作为选项之一,您可以使用Python的内置函数enumerate从行列表中获取每一行的索引,而不必将最大值添加到列表中,而是可以对其进行比较到当前的最大字数。
def get_largest_word():
# Setting initial variable values
current_max_word = ''
current_max_word_length = 0
current_max_word_line = None
with open('song.txt','r') as infile:
lines = infile.read().splitlines()
for line_index,line in enumerate(lines):
words = line.split()
max_word_in_line = max(words,key=len)
max_word_in_line_length = len(max_word_in_line)
if max_word_in_line_length > current_max_word_length:
# updating the largest word value with a new maximum word
current_max_word = max_word_in_line
current_max_word_length = max_word_in_line_length
current_max_word_line = line_index + 1 # line number starting from 1
print(current_max_word)
print(current_max_word_length)
print(current_max_word_line)
return current_max_word,current_max_word_length,current_max_word_line
P.S .:此功能不建议如何处理相同长度的行最大字,应选择其中哪个作为绝对最大。您需要相应地调整代码。
P.P.S .:此示例在Python 3中使用,因此如有必要,请更改代码段以使其在Python 2.7中可用。
,您知道吗?
- 许多相同长度的“最大”单词
- 几行包含最大长度的单词
这是找到一个最大单词并返回包含该单词的行数列表的代码:
# built a dictionary:
# line_num: largest_word_in_this_line
# line_num: largest_word_in_this_line
# etc...
# !!! actually,a line can contain several largest words
list_words = {}
with open('song.txt','r') as infile:
for i,line in enumerate(infile.read().splitlines()):
list_words[i] = max(line.split(),key=len)
# get the largest word from values of the dictionary
# !!! there can be several different 'largest' words with the same length
largest_word = max(list_words.values(),key=len)
# get a list of numbers of lines (keys of the dictionary) that contain the largest word
lines = list(filter(lambda key: list_words[key] == largest_word,list_words))
print(lines)
如果要获取所有具有最大长度相同的单词的行,则需要以这种方式修改代码中的最后两行:
lines = list(filter(lambda key: len(list_words[key]) == len(largest_word),list_words))
print(lines)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。