【问题标题】:Sorting multi-dictionary in python在python中对多字典进行排序
【发布时间】:2021-04-17 21:09:16
【问题描述】:

我有这样的数据结构:

poll = {
  'LINK' : {'MoonRaccoon' : 1, 'TheDirtyTree' : 1},
  'ZRX' : {'MoonRaccoon' : 1, 'Dontcallmeskaface' : 1, 'TheDirtyTree' : 1},  
  'XRP' : {'Dontcallmeskaface' : 1},
  'XLM' : {'aeon' : 1, 'Bob' : 1} 
}

我希望它最终按每个请求的人数排序,然后按字母顺序排列股票符号,然后按字母顺序排列用户

!pollresults

ZRX : Dontcallmeskaface, MoonRaccoon, TheDirtyTree
LINK : MoonRaccoon, TheDirtyTree
XLM : aeon, Bob
XRP: Dontcallmeskaface

任何真正擅长排序的人都可以帮助我做到这一点。我对 python 非常陌生,而且总体上对编码非常生疏。

感谢您的帮助。

【问题讨论】:

    标签: python associative-array


    【解决方案1】:

    字典不能真正排序,但为了打印的目的,可以这样做。

    
    poll = {
      'LINK' : {'MoonRaccoon' : 1, 'TheDirtyTree' : 1},
      'ZRX' : {'MoonRaccoon' : 1, 'Dontcallmeskaface' : 1, 'TheDirtyTree' : 1},  
      'XRP' : {'Dontcallmeskaface' : 1},
      'XLM' : {'aeon' : 1, 'Bob' : 1} 
    }
    
    def print_polls(poll):
        for ticker in sorted(poll, key=lambda t: sum(poll[t].values()), reverse=True):
            print(f"{ticker}: {', '.join(sorted(poll[ticker]))}")
    

    这将为您提供您正在寻找的输出

    【讨论】:

    • 谢谢。我不认为我想要Bobaeon 之前。我希望它最好按字母顺序排序。我会试试看。
    • TypeError: join() 没有关键字参数
    • @RobinGoodheart 只是摆脱钥匙。我把括号放在错误的位置
    • 我需要将它放在一个字符串中,以便将其发送到不和谐聊天中。而不是打印,我可以只说结果 += 并返回结果以将其发送到不和谐输出吗?
    • @RobinGoodheart 确定。您将要在字符串末尾添加一个 \n 字符以格式化多行
    【解决方案2】:
    1. 点票poll,得到d
    2. d递减排序
    3. 按步骤 2 的顺序获取投票结果,并处理名单排序
    d = [[k, len(v)] for k, v in poll.items()]
    d.sort(key=lambda name_vote: (-name_vote[-1],name_vote[0]))
    pollresults = [name + ' : ' + ', '.join(sorted(poll[name].keys(), key=str.lower)) for name,vote in d]
    

    结果:

    >>> pollresults
    ['ZRX : Dontcallmeskaface, MoonRaccoon, TheDirtyTree', 'LINK : MoonRaccoon, TheDirtyTree', 'XLM : aeon, Bob', 'XRP : Dontcallmeskaface']
    

    【讨论】:

      【解决方案3】:

      这里有一个 oneliner:

      print (sorted(poll.items(), key = lambda item : len(list(item[1].keys())), reverse = True))
      

      输出:

      [('ZRX', {'MoonRaccoon': 1, 'Dontcallmeskaface': 1, 'TheDirtyTree': 1}), ('LINK', {'MoonRaccoon': 1, 'TheDirtyTree': 1}), ('XLM', {'aeon': 1, 'Bob': 1}), ('XRP', {'Dontcallmeskaface': 1})]
      

      为了漂亮的打印:

      lst = sorted(poll.items(), key = lambda item : len(list(item[1].keys())), reverse = True)
      
      for elem in lst:
          print (elem[0],":"," ".join(elem[1].keys()))
      

      因为我真的很喜欢 oneliners,所以一切都在一条线上!

      print ("\n".join([" : ".join([elem[0]," ".join(list(elem[1].keys()))]) for elem in sorted(poll.items(), key = lambda item : len(list(item[1].keys())), reverse = True)]))
      

      输出:

      ZRX : MoonRaccoon Dontcallmeskaface TheDirtyTree
      LINK : MoonRaccoon TheDirtyTree
      XLM : aeon Bob
      XRP : Dontcallmeskaface
      

      【讨论】:

      • 好,如果有帮助,请考虑接受我的回答;)
      • 我愿意,但我已经接受了我之前使用的回复。但我也喜欢你的回复。
      • 这个真的很好..我要去试试。
      • 非常感谢您提供最优雅的解决方案。我可以请您回答另一个问题吗? stackoverflow.com/questions/65695667/…
      • 再次感谢您的帮助。你完全正确,因为 Repl 数据库可能是某种哈希表平面文件,你必须覆盖它。我还有一个挑战要给你。你能在自动收报机上按字母顺序进行预排序,以便投票数相同的人按字母顺序排列吗?
      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2017-07-12
      • 2014-11-10
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      相关资源
      最近更新 更多