prints line number in both txtfile and list????

Posted by jad on Stack Overflow See other posts from Stack Overflow or by jad
Published on 2010-05-24T07:29:01Z Indexed on 2010/05/24 8:21 UTC
Read the original article Hit count: 222

Filed under:

i have this code which prints the line number in infile but also the linenumber in words what do i do to only print the line number of the txt file next to the words???

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)

for stuff in wrongwords: print(stuff, d[stuff])

the output is :

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]

© Stack Overflow or respective owner

Related posts about python