【问题标题】:python: How to sort a dict with first based on value (can be empty/None/True/False) and then based on Keypython:如何首先根据值(可以为空/无/真/假)然后基于键对字典进行排序
【发布时间】:2019-10-06 21:47:33
【问题描述】:

我有以下字典:

所有值要么是字符串,要么是 None,要么是空,要么是布尔值(真/假)

{
 'id': None,
 'last_login': None,
 'is_superuser': False,
 'is_staff': False,
 'date_joined': '2019-10-06',
 'password': 'pbkdf2_sha256$150000$zXmhAd1NIgI4$1wMGVVDGTSCXZmdFtIoIB/aB3/4nfMn6DnYIuXeRyr8=',
 'last_login_passwd': None,
 'last_login_otp': None,
 'last_password_change': None,
 'first_name': '',
 'last_name': '',
 'email': '',
 'is_active': False,
 'otp_pass_change': '',
 'first_otp_passlogin_create': '',
 'first_otp_otplogin_create': '',
 'about': '',
 'location': '',
 'birth_date': None,
 'first_otp_login_created_date': None,
 'first_pass_login_created_date': None,
 'modified_date': None}

现在我想看看这个字典先根据值排序,然后再根据键排序。

{
     'is_active' : FALSE,
     'is_staff' : FALSE,
     'is_superuser' : FALSE,
     'about' : '',
     'email' : '',
     'first_name' : '',
     'first_otp_otplogin_create' : '',
     'first_otp_passlogin_create' : '',
     'last_name' : '',
     'location' : '',
     'otp_pass_change' : '',
     'password' : 'pbkdf2_sha256$150000$zXmhAd1NIgI4$1wMGVVDGTSCXZmdFtIoIB/aB3/4nfMn6DnYIuXeRyr8=',
     'date_joined' : '2019-10-06',
     'birth_date' : None,
     'first_otp_login_created_date' : None,
     'first_pass_login_created_date' : None,
     'id' : None,
     'last_login_otp' : None,
     'last_login_passwd' : None,
     'last_login' : None,
     'last_password_change' : None,
     'modified_date' : None,
}

我该怎么做。

我试过了:

test =     {
     'id': None,
     'last_login': None,
     'is_superuser': False,
     'is_staff': False,
     'date_joined': '2019-10-06',
     'password': 'pbkdf2_sha256$150000$zXmhAd1NIgI4$1wMGVVDGTSCXZmdFtIoIB/aB3/4nfMn6DnYIuXeRyr8=',
     'last_login_passwd': None,
     'last_login_otp': None,
     'last_password_change': None,
     'first_name': '',
     'last_name': '',
     'email': '',
     'is_active': False,
     'otp_pass_change': '',
     'first_otp_passlogin_create': '',
     'first_otp_otplogin_create': '',
     'about': '',
     'location': '',
     'birth_date': None,
     'first_otp_login_created_date': None,
     'first_pass_login_created_date': None,
     'modified_date': None}
sorted(test.items(), key=lambda kv: kv[1], reverse=True)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-453676f7d0ed> in <module>
     22      'first_pass_login_created_date': None,
     23      'modified_date': None}
---> 24 sorted(hare.items(), key=lambda kv: kv[1], reverse=True)

TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'

【问题讨论】:

  • 我要排序看看。它不是用于编程目的
  • sorted(test.items(), key=lambda kv: (str(kv[1]), kv[0]), reverse=True) 允许按值和键排序,但确实不产生您想要的排序顺序。注意:将具有不同类型的值转换为字符串以允许排序。
  • @cricket_007 字典从 CPython 3.6 开始排序,并且在 Python 3.7 之后的所有其他 Python 实现中正式排序

标签: python sorting dictionary


【解决方案1】:

如下使用str(kv[1])

test =     {
 'id': None,
 'last_login': None,
 'is_superuser': False,
 'is_staff': False,
 'date_joined': '2019-10-06',
 'password': 'pbkdf2_sha256$150000$zXmhAd1NIgI4$1wMGVVDGTSCXZmdFtIoIB/aB3/4nfMn6DnYIuXeRyr8=',
 'last_login_passwd': None,
 'last_login_otp': None,
 'last_password_change': None,
 'first_name': '',
 'last_name': '',
 'email': '',
 'is_active': False,
 'otp_pass_change': '',
 'first_otp_passlogin_create': '',
 'first_otp_otplogin_create': '',
 'about': '',
 'location': '',
 'birth_date': None,
 'first_otp_login_created_date': None,
 'first_pass_login_created_date': None,
 'modified_date': None}

print(sorted(test.items(), key=lambda kv: str(kv[1]), reverse=True))

【讨论】:

    【解决方案2】:

    根据THIS,无法比较不同的类型,它会引发 TypeError。你可以这样做:

    test = dict(sorted(test.items(), key=lambda t: (str(t[1]), str(t[0]))))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      相关资源
      最近更新 更多