views:

66

answers:

1

i have this piece of code the last bit of the code starting from d = {} im trying to print the words with its line number located in the text but it is not working its only printing the words anyone know why ??? need help ASAP

import sys
import string

text = []
infile = open(sys.argv[1], 'r').read()
for punct in string.punctuation:
    infile = infile.replace(punct, "")
    text = infile.split("\n")

dict = open(sys.argv[2], 'r').read()
dictset = []
dictset = dict.split()

words = []
words = list(set(text) - set(dictset))
words = [text.lower() for text in words]
words.sort()

d = {}
counter = 0
for lines in text:
    counter += 1
if word not in d:
    d[words] = [counter]
else:
    d[words.append[counter]
print(word, d)

this code outputs:

helo
goin
ist

i want it to output :

helo #tab# 3   4 
goin #tab# 1   2
+1  A: 

text is a list of WORDS, it's not a list of LINES. When you do:

text = infile.split()

you're irreversibly, forever throwing away all connections between a word and the line it was in. So when you later write

for lines in text:

it's a lie: text's items are words, not lines. If they weren't, then this other earlier line:

 words = list(set(text) - set(dictset))

would be totally broken -- this depends on text's items being words, not lines.

And, by the way, when you do:

  words = [text.lower() for text in words]

text is now left bound to the last item in words -- you've destroyed whatever other value it had previously.

Recommendation number one: stop reusing identifiers for many different, incompatible purposes. Make a commitment to yourself that no identifier shall ever be bound to two different things within any one of your programs. This will, at least, reduce the incredible amount of utter confusion that you manager to pile onto so few lines.

Alex Martelli
Or, worse, `dict`...
Ken
so do i remove text = infile.split()??????????????/
jad
@jad, you rewrite from scratch again, with the vow of never, **ever** reusing an identifier for different things. You can't just avoid making `text` a list of words because as I pointed out you _have_ to have it that way when you make a `set` out of it -- but you need a list of lines instead (splitlines, not split) when you need the line numbers. So clearly you can't use a single identifier `text` for both purposes (and more besides, e.g. control variable in that list comprehension). Use different, clearer, unambiguous identifiers and rethink your homework from scratch.
Alex Martelli
i just want a way to print the words and the line number of the word in the text
jad
i have changed text = infile..... will this work now ?
jad
No it won't, now you've broken `words = list(set(text) - set(dictset))` which as I mentioned relies on `text` being words and not lines (which of course makes it impossible to get line numbers). I understand perfectly well what you want, but this being homework it would be ethically wrong for me to just give you the right code, even though it would take me much less time than I've burned on this so far. You **must think for yourself**: you need a list of lines, otherwise line numbers can't be shown, so how do you fix that listcomp I've mentioned so many times already?
Alex Martelli
so do i have to do this from the beginning if u said a list of lines what do i do?text = lines.append()ive been programming for 2months and u would know how much i would. syntx is my main problem i understand how to do it in english but dont know much syntax because of my exp which = 2 months
jad
if i can see how it is done i bet u i would understand it but i dont know how becaus this is the first program i have ever done in my life
jad
@jad, if `text` is a list of lines, to make a set of _words_ from it you can use `set(w for line in text for w in line.split())`. Hint enough for you? If you write out in editing your answer a correct and complete description of the solution in English, the Python syntax is the least of the issues -- but so far you have not shown a complete and correct solution in *any* language.
Alex Martelli