当前位置: 首页 > 知识库问答 >
问题:

与行号一致

娄飞鸾
2023-03-14

编写一个程序,创建一个协调文件——一个索引,告诉你每个单词出现在文件的哪一行。调用函数concord,并接受输入文件名作为参数。将输出写入名为concord的文件。txt。如果一个单词出现在多行中,则一致性将显示包含该单词的所有行。提示:使用由每个单词组成的字典来解决这个问题。

输入文件包含:

I went to the restaurant yesterday.  Hello, I said, to the man who
greeted me at the door.  Where is your restroom?  On my way to the
restroom, I bumped into the waiter boy.  Excuse me, sir, I said.
When I returned to the table, the meal was served.  These are the
best clams I have ever eaten, I said.  My compliments to the chef.
Unfortunately, I was arrested by the officer for not paying my bill.

生产:

clams  [5]
   is  [2]
 chef  [5]
 ever  [5]
   at  [2]
 have  [5]
table  [4]
 your  [2]
 best  [5]
  sir  [3]
 said  [1, 3, 5]
  for  [6]
  boy  [3]
 when  [4]
   by  [6]
   to  [1, 2, 4, 5]
  way  [2]
  was  [4, 6]
  ...

L08-8)(5分)与上述代码相同,但在打印时应对索引中的单词进行排序。

到目前为止,我已经知道了,但它给出的是单词出现的次数,而不是行号。

Python代码:

def main():
   """
       Main function
   """
   try:
       # Opening file for reading
       fp = open("d:\Python\input.txt");

       # Dictionary to hold words and their frequencies
       wordDict = {};

       # Reading data line by line
       for line in fp:
           # Splitting words on space
           words = line.split(" ");

           # Looping over each word in words
           for word in words:

               # Considering only the words with length at-least 1
               if len(word) > 0:
                   # Converting to lower case
                   word = word.lower();

                   # Checking for existence of key in dict
                   if word in wordDict.keys():
                       # If already present, just update frequency
                       wordDict[word] += 1;
                   else:
                       # If new word, updating existing value
                       wordDict[word] = 1;

       # Closing file
       fp.close();

       # Looping over sorted keys of dictionary
       for key in sorted(wordDict):
           # Printing word frequency values
           print(" {0} : {1} ".format(key, wordDict[key]));

   except Exception as ex:
       # Handling exceptions
       print(ex);

# Calling main function      
main();

共有2个答案

姬经义
2023-03-14

因为俏皮话很酷。

s = """I went to the restaurant yesterday. Hello, I said, to the man who
greeted me at the door. Where is your restroom? On my way to the
restroom, I bumped into the waiter boy. Excuse me, sir, I said.
When I returned to the table, the meal was served. These are the
best clams I have ever eaten, I said. My compliments to the chef.
Unfortunately, I was arrested by the officer for not paying my bill."""

words = {word.lower().strip(' .,!?'): [l_n+1 for l_n, l in enumerate(s.split('\n')) if word.lower().strip(' .,!?') in [w.lower().strip(' .,!?') for w in l.split()]] for line in s.split('\n') for word in line.split()}

为了便于阅读:

words = {
    word.lower().strip(' .,!?'): [l_n+1 for l_n, l in enumerate(s.split('\n')) 
                                  if word.lower().strip(' .,!?') in [w.lower().strip(' .,!?') 
                                                                     for w in l.split()]]
    for line in s.split('\n')
    for word in line.split()
}
景志
2023-03-14

尝试创建一个字典,其中键是单词,值是单词所在的行:

s = """I went to the restaurant yesterday. Hello, I said, to the man who
greeted me at the door. Where is your restroom? On my way to the
restroom, I bumped into the waiter boy. Excuse me, sir, I said.
When I returned to the table, the meal was served. These are the
best clams I have ever eaten, I said. My compliments to the chef.
Unfortunately, I was arrested by the officer for not paying my bill."""

def main():
    words = {}
    lines = [i.lower() for i in s.split("\n")]
    for line in lines:
        for word in line.split():
            w = word.strip(" ,.!?")
            words[w] = [i for i, l in enumerate(lines, start=1) if w in l]

    for w, n in words.items():
        print(w, n)
main()
 类似资料:
  • 我的应用程序崩溃了,我的设备上有一个NPE。在使用ACRA接收的堆栈跟踪中,行号引用了两个源代码类和。对于,没有一个行号与任何源代码版本中的行号匹配。我的设备没有自定义ROM。这是一款运行Android4.03的Galaxy S2。 下面是堆栈跟踪: 如果查看不同版本的源代码,您可以自己检查数字是否匹配。 例如,在Android4.03的源代码中,位于第1892行,而我的堆栈跟踪显示它位于第202

  • 本文向大家介绍asp.net获取ListView与gridview中当前行的行号,包括了asp.net获取ListView与gridview中当前行的行号的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了asp.net获取ListView与gridview中当前行的行号。分享给大家供大家参考,具体如下: aspx中,在gridview/ListView中,有一模板列,就叫linkbutton

  • 我读过很多关于树集、可比/比较器接口、equals、compareTo、compare方法的帖子,我知道API说您必须使您的排序“与equals一致”,否则可能会发生奇怪的事情。 但在我的情况下,我认为这是一个相当普遍的情况,我真的需要一个“与等于不一致”的TreeSet排序。 假设我们正在进行某种启发式搜索,并且我们正在从根(初始)状态开始扩展(或生成)新状态。我们将新的(扩展/生成的)状态放入

  • 有人能帮我把我的x记号设置成横条吗。如图所示,条形图与xtick time值不一致。我已经在下面打印了、的数据值以及代码。我已经尝试过这个解决方案,Python MatplotLib将第一个x轴值标记为1(而不是0),,,尽管随后的条形图与x记号一致,但它变为数字1到28。我想要我的图像中的时间段。

  • 问题内容: 手动编写HTML时,我总是使用单引号。我使用很多渲染的HTML,这些HTML总是使用双引号。这使我可以确定HTML是手工编写的还是生成的。这是一个好主意吗? 两者有什么区别?我知道它们都可以工作,并且得到所有现代浏览器的支持,但是在不同情况下,一个实际上比另一个更好吗? 问题答案: W3组织说: 默认情况下,SGML要求使用双引号(ASCII十进制34)或单引号(ASCII十进制39)

  • 我是gradle的新手,目前只是试图遵循教程,有很多次我看到单引号和双引号混杂在一起。我只是想知道是否有一个不同的时间应该使用一套比另一个。这方面的一个例子是教程的第6.12节--默认任务: 所以,我只想知道我是否应该注意这些差异,或者它们是否可以相互改变,我可以使用单引号或双引号时打印字符串在Gradle。