Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. I am stuck on this python assignment. Don't need the answer asap, just some explaining of how to write code to do that. I know about the built in sort function

Respuesta :

Answer:

Read the file and save it to a variable as a string, split the string variable and cast it to a set object using the set function to get the unique words in the file. Then use the max function with the key attribute to use regular expression module "re" to compare the first letter of each item to return in a list comprehension or append to a list.

Explanation:

The open function is used to import a file in python, the split string method splits the string to a list of items and the set function removes any duplicates of a word or item.

Using the for loop statement, iterate over the items and compare and return the items in alphabetical order with the 're' search method getting the item with uppercase letters with higher precedence than lowercase.