在文本文件和列表中打印行号?

时间:2010-05-24 07:29:02

标签: python

我有这个代码打印在infile中的行号,但也用字母打印字母我该怎么做只打印单词旁边的txt文件的行号???

d = {}
counter = 0
wrongwords = []
for line in infile:
infile = line.split()
wrongwords.extend(infile)
counter += 1
for word in infile:
    if word not in d:
        d[word] = [counter]
    if word in d:
        d[word].append(counter)

用于错误词中的内容:     print(stuff,d [stuff])

输出是:

hello    [1,  2,  7,  9] # this is printing the linenumber of the txt file
hello    [1] # this is printing the linenumber of the list words
hello    [1]
what i want is:
hello    [1,  2,  7,  9]

1 个答案:

答案 0 :(得分:0)

四件事:

  1. 您可以通过执行此操作来跟踪行号而不是处理 反击你自己:

    for line_no, word in enumerate(infile):
    
  2. 正如sateesh上面指出的那样,你可能需要一个else 条件:

    if word not in d:
        d[word] = [counter]
    else:
        d[word].append(counter)
    
  3. 另请注意,上面的代码段正是defaultdict的内容 为:

    from collections import defaultdict
    d = defaultdict(list)
    

    然后在你的主循环中,你可以摆脱if..else部分:

    d[word].append(counter)
    
  4. 你为什么要wrongwords.extend(infile)

  5. 另外,我真的不明白你应该如何决定“错误的词汇”。我假设你有一个名为wrongwords的集合包含错误的单词,这使得你的最终代码如下:

    from collections import defaultdict
    d = defaultdict(list)
    wrongwords = set(["hello", "foo", "bar", "baz"])
    for counter, line in enumerate(infile):
        infile = line.split()
        for word in infile:
            if word in wrongwords:
                d[word].append(counter)