On 12/20/18 8:49 AM, Mary Sauerland wrote: > Hi, > > I want to get rid of words that are less than three characters but I keep > getting errors. I tried multiple ways but keep getting errors.
Just a quick note or two: > > Here is my code: > > f1_name = "/Users/marysauerland/Documents/file1.txt" > #the opinions > f2_name = "/Users/marysauerland/Documents/file2.txt" > #the constitution > > > def read_words(words_file): > return [word.upper() for line in open(words_file, 'r') for word in > line.split()] > > > read_words(f1_name) ^^^ this line is meaningless. "everything is an object" in Python. your function returns a list object - which you don't do anything with. you should assign a name to it, like: constitution_words = read_words(f1_name) Since no name is assigned to that object, Python sees it has no references, and it is just lost. and then... > #performs the function on the file > set1 = set(read_words(f1_name)) if you saved the object returned from the earlier call to the function, then you don't need to call the function again, instead you convert the saved list object to a set object. We can't tell whether you have an eventual use for the unfiltered list of words, or only the set of unique words, the answer to that determines how you write this section. picking a more descriptive name than set1 would be a good idea (and f1_name as well, and others - when writing software, the hard part is maintenance, where you or others have to go in later and fix or change something. using meaningful names really helps with that, so it's a good habit to get into). since you have sets consisting of words from your two documents, you may as well use set operations to work with them. Do you know the set operation to find all of the members of one set that are also in another set? hint: in set theory, that is called the intersection. you say you are trying to remove short words, but there seems to be no code to do that. instead you seem to be solving a different problem? _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor