计算字符串长度
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 对列表按元组指定列进行排序
|