Apr-13-2017, 09:18 PM
I have a text file that is going to be sent to my function. I simply need to send the words in the file back in a set. Not so difficult with a for loop. My for loop is like this:
When I try to do it with a set comprehension, I'm doing this:
#the text file is sent to my function. This is the only code in it. for w in text.split(): w.strip(punctuation) cypher.add(w) print(cypher) return cypherThe print statement is just so I can test it and make sure I did it right. This works just fine and I return the words. I need them in a set using set comprehension. I'm missing something and I'm trying to put my for loop into a comprehension, but don't know how. I've looked at multiple examples but can't figure out what I'm doing wrong.
When I try to do it with a set comprehension, I'm doing this:
#The text file is sent to my function. Only code is below: cypher = set() cypher = {text.strip(punctuation()) for w in text.split()} return cypherThanks for any help.