首页 > 解决方案 > 如何对具有复杂字符串的字母数字列表进行排序

问题描述

我需要你的帮助,因为我遇到了这个问题。我想对一个包含丑陋字符串和整数的列表进行排序。我想要的顺序如下:从

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']

标签: pythonlist

解决方案


我们可以使用正则表达式自定义排序键的组合来实现所需的结果。

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))']

推荐阅读