简体   繁体   中英

Need Help. How to extract list from .txt and sort with input

Okay So, i have a dilemma. I have a document filled with names of countries. Now i need to get the list in python with open. Done that. However I am required to make a program that inputs a letter and gives all the countries that start with that letter. cant seem to figure it out. Any help would be appreciated

def getcountries(country_list, letter):
    output = []
    for x in country_list:
        if x[:1].lower() == letter.lower():
            output.append(x)
            print (x)
    return(output)

pass this function the list of country names, and the letter to search for. it will print the name of each country, and will return a list of those countries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM