Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的列表比较_Python_List_Sorting_Sorted - Fatal编程技术网

Python中的列表比较

Python中的列表比较,python,list,sorting,sorted,Python,List,Sorting,Sorted,因此,对于我的程序,我有两个定义,get_codes和get_data,我创建了8个列表,如下所示: CountryCodes=open("CountryCodes.csv",'r') CountryData=open("CountryData.csv", 'r') def get_codes(file): country_code=[] country_name=[] continent=[] for line in CountryCodes:

因此,对于我的程序,我有两个定义,get_codes和get_data,我创建了8个列表,如下所示:

CountryCodes=open("CountryCodes.csv",'r')
CountryData=open("CountryData.csv", 'r')

def get_codes(file):
    country_code=[]
    country_name=[]
    continent=[]

    for line in CountryCodes: 
        c_fields=line.split(",")

        c_field1=c_fields[0].strip()
        c_field2=c_fields[1].strip()
        c_field3=c_fields[2].strip() 

        country_code.append(c_field1)
        country_name.append(c_field2)
        continent.append(c_field3)

    return country_code, country_name, continent

def get_data(file): 
    data_country_code=[]
    country_pop=[]
    country_area=[]
    country_gdp=[]
    country_lit_rate=[]

    for line in CountryData: 
        d_fields=line.split(",")

        d_field1=d_fields[0].strip()
        d_field2=d_fields[1].strip()
        d_field3=d_fields[2].strip()
        d_field4=d_fields[3].strip()
        d_field5=d_fields[4].strip()

        data_country_code.append(d_field1)
        country_pop.append(int(d_field2))
        country_area.append(d_field3)
        country_gdp.append(int(d_field4))
        country_lit_rate.append(d_field5)

    return data_country_code, country_pop, country_area, country_gdp, country_lit_rate
现在我要做的是创建一个菜单选项(我有菜单),让国家/地区按升序排列,然后按降序排列。以下是我目前掌握的情况:

def asc_sort():
    for x in range (0,len(country_pop)):
        country_pop2=sorted(country_pop)
我已经整理好了,但我的教授不想只打印国家。她还想要国家名,这是在国家代码中,而不是在人口所在的国家数据中。因此,国家/地区pop的索引x在数据/国家/地区代码中也应该是x。然后我需要在数据\国家\代码中输入x,假设它是AL,然后在国家\代码中找到它。接下来,我必须找到对应的国家名称阿尔巴尼亚,对应国家代码AL,并列出国家名称和国家流行名称,我假设如下:

print("%-10s \t %-10s \t" %("NAMES","POPULATION"))
for ind in range (0,len(list1)):
    if ind%10==9:
        input("Press ENTER to continue")
    print("%-2s \t %-10s \t %-10s \t %-10s \t %-10s \t" %(I'd need help here)

(我需要%-10s部分用于格式化和if语句,因为我的列表太长了,一次只想显示几个)任何帮助都将不胜感激,如果有人需要更多解释,我将尽全力

您需要做两件事:

  • 将CountryData中的所有数据存储在元组中的一行中,而不是单独的列表。这样,当您重新排列排序中的所有内容时,您也会重新排列县代码

  • 将CountryCodes中的数据存储在dict中,以便您可以从一个代码到另一个国家的名称

  • 我不确定我是否应该为你做所有的家庭作业,但如果你有这样的数据:

    country_data = [('uk', 123), ('usa', 42), ('cl', 99)]
    country_names = {'uk': 'united kingdom', 'usa': 'united states', 'cl': 'chile'}
    
    然后,这将为您提供按数字排序的国家名称:

    sorted_data = sorted(country_data, key=lambda data: data[1])
    for (code, value) in sorted_data:
        print(country_names[code], value)
    

    注意
    键如何挑选元组的第二个元素进行排序。因此,对于
    ('uk',123)
    它将给出
    123
    ,这就是您要排序的内容。

    您需要做两件事:

  • 将CountryData中的所有数据存储在元组中的一行中,而不是单独的列表。这样,当您重新排列排序中的所有内容时,您也会重新排列县代码

  • 将CountryCodes中的数据存储在dict中,以便您可以从一个代码到另一个国家的名称

  • 我不确定我是否应该为你做所有的家庭作业,但如果你有这样的数据:

    country_data = [('uk', 123), ('usa', 42), ('cl', 99)]
    country_names = {'uk': 'united kingdom', 'usa': 'united states', 'cl': 'chile'}
    
    然后,这将为您提供按数字排序的国家名称:

    sorted_data = sorted(country_data, key=lambda data: data[1])
    for (code, value) in sorted_data:
        print(country_names[code], value)
    

    注意
    键如何挑选元组的第二个元素进行排序。因此,对于
    ('uk',123)
    ,它将给出
    123
    ,这就是您要排序的内容。

    我认为以下代码符合您的要求:

    with open("CountryCodes.csv",'r') as CountryCodes:
        genc = (line.split(',') for line in CountryCodes)
    
        c2n = dict((c_fields[0].strip(),c_fields[1].strip())
                   for c_fields in genc)
    
    with open("CountryData.csv",'r') as CountryData:
        gend = (line.split(',') for line in CountryData)
    
        the_data = [ (c2n[d_fields[0].strip()], # data_country_code
                      int(d_fields[1]),         # country_pop
                      d_fields[2].strip(),      # country_area
                      d_fields[3].strip(),      # country_gdp
                      d_fields[4].strip())      # country_lit_rate
                     for d_fields in gend ]
    
    the_data.sort(key = lambda x: x[1])
    
    p = 10
    for i in xrange(0,len(the_data),p):
        if i:  raw_input("  Press ENTER to continue\n")
        print ('\n'.join("%-10s \t %-10s \t %-10s \t %-10s \t %s"
                         % el for el in the_data[i:i+p]) )
    
    字典c2n给出了与代码对应的名称

    population字段当然是一个字符串,表示一个不需要分条的整数,即使其中有空格(空格、制表符……而不是换行符)

    编辑 如果您无权使用字典,但只能使用列表,您可以这样做:

    with open("CountryCodes.csv",'r') as CountryCodes:
        genc = (line.split(',') for line in CountryCodes)
        lic = [map(str.strip,row) for row in genc]
    
    def name_of(x,lic=lic):
      for code,name,continent in lic:
        if x==code:
          return name
    
    with open("CountryData.csv",'r') as CountryData:
        gend = (line.split(',') for line in CountryData)
    
        the_data = [ (name_of(d_fields[0].strip()), # data_country_code
                      int(d_fields[1]),         # country_pop
                      d_fields[2].strip(),      # country_area
                      d_fields[3].strip(),      # country_gdp
                      d_fields[4].strip())      # country_lit_rate
                     for d_fields in gend ]
    

    我认为以下代码符合您的要求:

    with open("CountryCodes.csv",'r') as CountryCodes:
        genc = (line.split(',') for line in CountryCodes)
    
        c2n = dict((c_fields[0].strip(),c_fields[1].strip())
                   for c_fields in genc)
    
    with open("CountryData.csv",'r') as CountryData:
        gend = (line.split(',') for line in CountryData)
    
        the_data = [ (c2n[d_fields[0].strip()], # data_country_code
                      int(d_fields[1]),         # country_pop
                      d_fields[2].strip(),      # country_area
                      d_fields[3].strip(),      # country_gdp
                      d_fields[4].strip())      # country_lit_rate
                     for d_fields in gend ]
    
    the_data.sort(key = lambda x: x[1])
    
    p = 10
    for i in xrange(0,len(the_data),p):
        if i:  raw_input("  Press ENTER to continue\n")
        print ('\n'.join("%-10s \t %-10s \t %-10s \t %-10s \t %s"
                         % el for el in the_data[i:i+p]) )
    
    字典c2n给出了与代码对应的名称

    population字段当然是一个字符串,表示一个不需要分条的整数,即使其中有空格(空格、制表符……而不是换行符)

    编辑 如果您无权使用字典,但只能使用列表,您可以这样做:

    with open("CountryCodes.csv",'r') as CountryCodes:
        genc = (line.split(',') for line in CountryCodes)
        lic = [map(str.strip,row) for row in genc]
    
    def name_of(x,lic=lic):
      for code,name,continent in lic:
        if x==code:
          return name
    
    with open("CountryData.csv",'r') as CountryData:
        gend = (line.split(',') for line in CountryData)
    
        the_data = [ (name_of(d_fields[0].strip()), # data_country_code
                      int(d_fields[1]),         # country_pop
                      d_fields[2].strip(),      # country_area
                      d_fields[3].strip(),      # country_gdp
                      d_fields[4].strip())      # country_lit_rate
                     for d_fields in gend ]
    

    嗯,这个问题现在写出来的方式,你不太可能得到太多的帮助。。试着阅读关于s的文章,并相应地修改你的问题。e、 例如,不是给我们提供读取文件的代码,而是给我们提供使用字符串并从中读取的代码。你确定国家代码(country_code)需要拆分吗?你的做法不太好。CountryData.csv文件的每一行都包含与一个国家有关的数据。您的算法所做的是将一个国家的数据分离到不同的列表中,那么您在收集数据时就会遇到问题每个国家的代码和名称都有同样的错误:你把它们分开。你必须做的是在代码和相应的名称之间保持一个链接:这是使用字典完成的。克劳迪乌,文件中的信息列出了数百个国家,程序已经进行了很多事情,我很紧张要修改,但谢谢你的输入!我不打算为你做家庭作业,但我可以告诉你,如果你仔细阅读,这个练习会容易得多。嗯,这个问题现在写出来的方式,你不可能得到太多帮助。。试着阅读关于s的文章,并相应地修改你的问题。e、 例如,不是给我们提供读取文件的代码,而是给我们提供使用字符串并从中读取的代码。你确定国家代码(country_code)需要拆分吗?你的做法不太好。CountryData.csv文件的每一行都包含与一个国家有关的数据。您的算法所做的是将一个国家的数据分离到不同的列表中,那么您在收集数据时就会遇到问题每个国家的代码和名称都有同样的错误:你把它们分开。你必须做的是在代码和相应的名称之间保持一个链接:这是使用字典完成的。克劳迪乌,文件中的信息列出了数百个国家,程序已经进行了很多事情,我很紧张要修改,但谢谢你的输入!我不打算为你做家庭作业,但我可以告诉你,如果你仔细阅读,这个练习会容易得多。你好。当我发布我的评论时,我还没有看到你的答案!谢谢你,安德鲁,我已经像你建议的那样把我的数据放进一个元组和dict,我的教授没有向我们介绍它们,所以这是一个巨大的帮助!你好当我发布我的评论时,我还没有看到你的答案!谢谢你,安德鲁,我已经像你建议的那样把我的数据放进一个元组和dict,我的教授没有向我们介绍它们,所以这是一个巨大的帮助!你说的分裂是指脱衣舞吗?我最初使用strip是因为文件中有各种各样的疯狂空格。我在打印时遇到了一个错误,请原谅