python中各种排序

计算字符串长度

def calculate_string_width(strs, pointSize):
    chinese_numb = 0
    other_numb = 0
    for c in strs:
        if ('\u4e00' <= c <= '\u9fa5'):
            chinese_numb += 1
        else:
            other_numb += 1

    string_width = chinese_numb * pointSize + other_numb * pointSize / 2

    return string_width

字典排序

input_data_contact_info_dict = {}

for key in list(input_data.keys()):
    if key in contact_info_filter:
        input_data_contact_info_dict[key] = input_data[key]
input_data_contact_info_dict_sort = {k : v for k, v in sorted(input_data_contact_info_dict.items(), \
                                                              key=lambda items: calculate_string_width(items[1], 1))}
print('model_contact_info_list sort before: ', model_contact_info_list)
model_contact_info_list.sort(key=lambda k: (int(k.get('y', 0)), k.get('width', 0)))
print('model_contact_info_list sort after: ', len(model_contact_info_list), model_contact_info_list)

参考资料
python按字符串长度进行排序
python list元素为dict时的排序
Python3 对列表按元组指定列进行排序
python Map 排序
python 多维list 排序_python list排序的两种方法及实例讲解
Python3 对列表按元组指定列进行排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值