python - 如何对具有复杂字符串的字母数字列表进行排序
问题描述
我需要你的帮助,因为我遇到了这个问题。我想对一个包含丑陋字符串和整数的列表进行排序。我想要的顺序如下:从
f(-2,-2)=...,
f(-2,-1)=...,
f(-2,0)=...,
f(-2,1)=...,
f(-2,2)=...,
...,
f(2,2)= ...
我尝试使用内置函数 sorted 但没有帮助。有人可以帮助初学者。
list1 = ['f(-2,-2) = 0', 'f(-2,-1) = 0', 'f(-2,0) = 0', 'f(-1,-2) = 0', 'f(-1,-1) = 0', 'f(-1,0) = 0', 'f(0,-2) = 0', 'f(0,-1) = 0', 'f(-2,1) = scalar2_qp_1211(0,1)*(((d)*p11^2+((2*d+4)*m10^2+(-2*d)*m4^2)*p11+(d)*m10^4+((-2*d)*m4^2)*m10^2+(d)*m4^4)*den(d))', 'f(-2,2) = scalar2_qp_1211(0,1)*(((d-2)*p11^2+((2*d+4)*m10^2+(-2*d+4)*m4^2)*p11+(d+2)*m10^4+((-2*d)*m4^2)*m10^2+(d-2)*m4^4)*den(2*m10^2))', 'f(-1,1) = scalar2_qp_1211(0,1)*(p11+m10^2-m4^2)', 'f(-1,2) = scalar2_qp_1211(0,1)*(((d-2)*p11+(d)*m10^2+(-d+2)*m4^2)*den(2*m10^2))', 'f(0,1) = scalar2_qp_1211(0,1)*(1)', 'f(0,2) = scalar2_qp_1211(0,1)*((d-2)*den(2*m10^2))', 'f(1,-2) = scalar2_qp_016(1,0)*(((d)*p11^2+((-2*d)*m10^2+(2*d+4)*m4^2)*p11+(d)*m10^4+((-2*d)*m4^2)*m10^2+(d)*m4^4)*den(d))', 'f(1,-1) = scalar2_qp_016(1,0)*(p11-m10^2+m4^2)', 'f(1,0) = scalar2_qp_016(1,0)*(1)', 'f(1,1) = scalar2_qp_1216(1,1)*(1)', 'f(1,2) = scalar2_qp_016(1,0)*((d-2)*den(p11^2+(-2*m10^2-2*m4^2)*p11+m10^4+(-2*m4^2)*m10^2+m4^4))', 'f(2,-2) = scalar2_qp_016(1,0)*(((d-2)*p11^2+((-2*d+4)*m10^2+(2*d+4)*m4^2)*p11+(d-2)*m10^4+((-2*d)*m4^2)*m10^2+(d+2)*m4^4)*den(2*m4^2))', 'f(2,-1) = scalar2_qp_016(1,0)*(((d-2)*p11+(-d+2)*m10^2+(d)*m4^2)*den(2*m4^2))', 'f(2,0) = scalar2_qp_016(1,0)*((d-2)*den(2*m4^2))', 'f(2,1) = scalar2_qp_016(1,0)*(((d-2)*p11+(-d+2)*m10^2+(-d+2)*m4^2)*den((2*m4^2)*p11^2+((-4*m4^2)*m10^2-4*m4^4)*p11+(2*m4^2)*m10^4+(-4*m4^4)*m10^2+2*m4^6))', 'f(2,2) = scalar2_qp_016(1,0)*(((d-2)*p11^2+((-2*d+4)*m10^2+(-2*d^2+12*d-16)*m4^2)*p11+(d-2)*m10^4+((-2*d^2+12*d-16)*m4^2)*m10^2+(2*d^2-13*d+18)*m4^4)*den((2*m4^2)*p11^4+((-8*m4^2)*m10^2-8*m4^4)*p11^3+((12*m4^2)*m10^4+(8*m4^4)*m10^2+12*m4^6)*p11^2+((-8*m4^2)*m10^6+(8*m4^4)*m10^4+(8*m4^6)*m10^2-8*m4^8)*p11+(2*m4^2)*m10^8+(-8*m4^4)*m10^6+(12*m4^6)*m10^4+(-8*m4^8)*m10^2+2*m4^10))', 'f(0,0) = 0']
解决方案
我们可以使用正则表达式和自定义排序键的组合来实现所需的结果。
import ast
import re
pattern = re.compile('\([+-]?[0-9]*,[+-]?[0-9]*\)')
def custom_sort(item):
match = pattern.search(item).group(0)
x, y = ast.literal_eval(match)
return x, y
sorted(list1, key=custom_sort)
['f(-2,-2) = 0',
'f(-2,-1) = 0',
'f(-2,0) = 0',
'f(-2,1) = scalar2_qp_1211(0,1)*(((d)*p11^2+((2*d+4)*m10^2+(-2*d)*m4^2)*p11+(d)*m10^4+((-2*d)*m4^2)*m10^2+(d)*m4^4)*den(d))',
'f(-2,2) = scalar2_qp_1211(0,1)*(((d-2)*p11^2+((2*d+4)*m10^2+(-2*d+4)*m4^2)*p11+(d+2)*m10^4+((-2*d)*m4^2)*m10^2+(d-2)*m4^4)*den(2*m10^2))',
'f(-1,-2) = 0',
'f(-1,-1) = 0',
'f(-1,0) = 0',
'f(-1,1) = scalar2_qp_1211(0,1)*(p11+m10^2-m4^2)',
'f(-1,2) = scalar2_qp_1211(0,1)*(((d-2)*p11+(d)*m10^2+(-d+2)*m4^2)*den(2*m10^2))',
'f(0,-2) = 0',
'f(0,-1) = 0',
'f(0,0) = 0',
'f(0,1) = scalar2_qp_1211(0,1)*(1)',
'f(0,2) = scalar2_qp_1211(0,1)*((d-2)*den(2*m10^2))',
'f(1,-2) = scalar2_qp_016(1,0)*(((d)*p11^2+((-2*d)*m10^2+(2*d+4)*m4^2)*p11+(d)*m10^4+((-2*d)*m4^2)*m10^2+(d)*m4^4)*den(d))',
'f(1,-1) = scalar2_qp_016(1,0)*(p11-m10^2+m4^2)',
'f(1,0) = scalar2_qp_016(1,0)*(1)',
'f(1,1) = scalar2_qp_1216(1,1)*(1)',
'f(1,2) = scalar2_qp_016(1,0)*((d-2)*den(p11^2+(-2*m10^2-2*m4^2)*p11+m10^4+(-2*m4^2)*m10^2+m4^4))',
'f(2,-2) = scalar2_qp_016(1,0)*(((d-2)*p11^2+((-2*d+4)*m10^2+(2*d+4)*m4^2)*p11+(d-2)*m10^4+((-2*d)*m4^2)*m10^2+(d+2)*m4^4)*den(2*m4^2))',
'f(2,-1) = scalar2_qp_016(1,0)*(((d-2)*p11+(-d+2)*m10^2+(d)*m4^2)*den(2*m4^2))',
'f(2,0) = scalar2_qp_016(1,0)*((d-2)*den(2*m4^2))',
'f(2,1) = scalar2_qp_016(1,0)*(((d-2)*p11+(-d+2)*m10^2+(-d+2)*m4^2)*den((2*m4^2)*p11^2+((-4*m4^2)*m10^2-4*m4^4)*p11+(2*m4^2)*m10^4+(-4*m4^4)*m10^2+2*m4^6))',
'f(2,2) = scalar2_qp_016(1,0)*(((d-2)*p11^2+((-2*d+4)*m10^2+(-2*d^2+12*d-16)*m4^2)*p11+(d-2)*m10^4+((-2*d^2+12*d-16)*m4^2)*m10^2+(2*d^2-13*d+18)*m4^4)*den((2*m4^2)*p11^4+((-8*m4^2)*m10^2-8*m4^4)*p11^3+((12*m4^2)*m10^4+(8*m4^4)*m10^2+12*m4^6)*p11^2+((-8*m4^2)*m10^6+(8*m4^4)*m10^4+(8*m4^6)*m10^2-8*m4^8)*p11+(2*m4^2)*m10^8+(-8*m4^4)*m10^6+(12*m4^6)*m10^4+(-8*m4^8)*m10^2+2*m4^10))']
推荐阅读
- javascript - 使用 Object.values() 对对象进行排序
- java - Apache-poi:自动调整大小隐藏/截断单元格内值的小数点
- python - 通过字典将条件组合分配给数据框中的新列
- graph - 画面中日期的离散与连续(维度与度量)
- vue.js - 在 v-for 循环中使用时无法识别 v-model
- flutter - 更新持久存储失败 - Galaxy Tab S6/Linux
- python - Deepcopy 不会产生唯一的对象
- docusignapi - Go Live 的 DocuSign 包络校正和 DocuSign 限制(违规)
- java - 如何让程序在java中检查一个字母的实例?
- flutter - 如何在颤动中限制onLocationChanged