【问题标题】:Can`t call correctly function with sort in Python无法在 Python 中使用 sort 正确调用函数
【发布时间】:2020-04-26 20:46:42
【问题描述】:

所以,我有一个嵌套列表,我需要使用 def 按值排序。问题是,该函数使用键对列表进行排序,因为没有正确调用另一个函数。 它不断重复。

currencies=[['hryvna', '1 hryvna', '2005', '0.036'], ['dollar', '1 dollar', '2006', '1'], ['euro', '1 euro', '2007', '1.08'], ['Belarusian ruble', '1 Belarusian ruble', '2008', '0.39'], ['pound', '1 pound', '2009', '1.23']]
def atribute_choice():
    atribute=int(input("""Choose the atribute:
    1 - Currency name;
    2 - Currency denomination;
    3 - Currency year of release;
    4 - Currency rate to the dollar\n"""))
    atribute=atribute-1
    return atribute
def sorting(elem):
    return elem[atribute_choice()]
def currencies_sorting():
    currencies.sort(key=sorting)


currencies_sorting()
print (currencies)

【问题讨论】:

    标签: python list function sorting


    【解决方案1】:

    它不断要求输入的原因是您要求输入 2D 列表中的每一行。

    如果您只想询问一次,请将您的代码修改为:

    currencies = [['hryvna', '1 hryvna', '2005', '0.036'],
    ['dollar', '1 dollar', '2006', '1'],
    ['euro', '1 euro', '2007', '1.08'],
    ['Belarusian ruble', '1 Belarusian ruble', '2008', '0.39'],
    ['pound', '1 pound', '2009', '1.23']]
    
    def atribute_choice():
        atribute = int(input("""Choose the atribute:
        1 - Currency name;
        2 - Currency denomination;
        3 - Currency year of release;
        4 - Currency rate to the dollar\n"""))
        atribute = atribute - 1
        return atribute
    
    def sorting(elem):
        a = elem[attribute]
        return a
    
    def currencies_sorting():
        currencies.sort(key=sorting)
    
    
    attribute = atribute_choice()
    currencies_sorting()
    print(currencies)
    

    【讨论】:

      【解决方案2】:

      为每个正在排序的元素调用key 函数。您需要提前选择属性,然后在键中使用该值。

      因此,您想将该值传递给sorting,但在调用sorting 的位置(在内部,通过排序方法)不提供该值。你需要“绑定”它;我们可以使用标准库中的functools.partial 优雅地做到这一点:

      from functools import partial
      
      def sorting(choice, elem):
          return elem[choice]
      
      def currencies_sorting():
          attribute = attribute_choice()
          currencies.sort(key=partial(sorting, attribute))
      

      但事实证明,我们在标准库中有一个更直接的工具来解决这个问题:

      from operator import itemgetter
      
      def currencies_sorting():
          currencies.sort(key=itemgetter(attribute_choice()))
      

      或者您可以使用lambda 自己构建它,如另一个答案所示。

      【讨论】:

        【解决方案3】:

        你可以这样做:

        currencies=[['hryvna', '1 hryvna', '2005', '0.036'], ['dollar', '1 dollar', '2006', '1'], ['euro', '1 euro', '2007', '1.08'], ['Belarusian ruble', '1 Belarusian ruble', '2008', '0.39'], ['pound', '1 pound', '2009', '1.23']]
        def atribute_choice():
            atribute=int(input("""Choose the atribute:
            1 - Currency name;
            2 - Currency denomination;
            3 - Currency year of release;
            4 - Currency rate to the dollar\n"""))
            atribute=atribute-1
            return atribute
        
        def currencies_sorting():
            choice = atribute_choice()
            currencies.sort(key=lambda x: x[choice])
        
        
        currencies_sorting()
        print (currencies)
        

        您还可以找到更多关于排序的信息here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-05
          • 2017-02-21
          • 1970-01-01
          • 2017-09-23
          • 2021-09-03
          • 1970-01-01
          相关资源
          最近更新 更多