如何使用字典计算集合中不正确单词的行数 [英] How to count line number of incorrect words in a set using a dictionary

查看:64
本文介绍了如何使用字典计算集合中不正确单词的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有单词的txt文件,我不得不将不正确的单词打印到我拥有的单词中。

现在我需要在文本文件中找到错误单词的哪一行并将其打印为字典

例如它应该看起来像在一起:

togeher 3 4 #word是错误的单词,3和4是它在txt文件中的行号。


我知道我需要使用行计数器但不知道如何使用它


words = []#是我的txt文件

text1#是我的错误单词

i到目前为止已完成此操作:

d = {}#空字典

key = text1

value = linecounter#不知道将值赋给

I have a txt file with words in it and I had to print the incorrect words into a set which I have.
Now I need to find in which line the incorrect words are in the text file and print it as a dictionary
for e.g. it should look like together:
togeher 3 4 #word being the incorrect word and 3 and 4 the line number where it is located in the txt file.

I know I need to use a line counter but dont know how to use it

words = [] # is my txt file
text1 # is my set of incorrect words
i have done this so far:
d = {} # an empty dictionary
key = text1
value = linecounter # dont know what to assign the value to
for

推荐答案

假设您有一个函数正确(单词),如果单词不正确,则返回False。以下未经测试的代码将编译不正确单词的字典,其中单词是键,行号包含在与键相关联的列表中。
Assume you have a function correct(word) that returns False if a word is incorrect. The following untested code would compile a dictionary of the incorrect words where the words are keys and the line numbers are contained in a list associated with the keys.
展开 | 选择 | Wrap | 行号


@bvdet


欢呼你的回复我已经完成了这个
@bvdet
cheers for your reply ive done this
展开 | 选择 | Wrap | 行号


发布时请使用代码标签代码。


要打印字典的内容,请迭代字典并格式化键和值的打印。在这种情况下,值是一个行号列表,所以...
Please use code tags when posting code.

To print the contents of a dictionary, iterate on the dictionary and format the printing of the key and value. In this case, the value is a list of line numbers, so...
展开 | 选择 | Wrap | 行号


这篇关于如何使用字典计算集合中不正确单词的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆