简体   繁体   English

需要帮忙。 如何从 .t​​xt 中提取列表并使用输入进行排序

[英]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.现在我需要在 python 中使用 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.它将打印每个国家/地区的名称,并返回这些国家/地区的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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