Python 如何对具有复杂字符串的字母数字列表进行排序

Python 如何对具有复杂字符串的字母数字列表进行排序,python,list,Python,List,我需要你的帮助,因为我被这个问题困住了。 我想对一个由丑陋的字符串和整数组成的列表进行排序。我想要的顺序如下:从 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(-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']

如果您想使用内置的
排序
,我认为应该可以根据要应用的排序逻辑将字符串拆分为元组

以下是一个通用示例:

a=[((“c”,2),(“a”,2),(“a”,1),(“b”,4)]
打印(已排序(a))
这张照片是:

[('a',1),('a',2),('b',4),('c',2)]
在您的例子中,您可以将
“f(-2,1)”
拆分为
[“f”(“,-2,”,“,1,”)”]
,依此类推,这取决于您的条目有多复杂。

您可以使用Python模块根据这些有符号数字的自然顺序对列表进行排序

您可以使用以下方式安装它:

$ pip install natsort
(尽管具体的细节可能在你使用的是ValualEnV、VeV、PiPNV等方面有所不同,或者你是否在一个平台上需要额外的权限来安装模块平台,在这种情况下你可以考虑将它安装在你的家庭目录中。在网络上搜索,或者StAdvExcel,你肯定会找到如何安装Pyt的帖子。(请参阅您的设置。)

安装后,您可以通过以下方式导入:

from natsort import natsorted, ns
然后,您可以使用以下方法对列表进行排序:

natsorted(list1, alg=ns.SIGNED)
产生:

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

您可以很容易地将排序顺序与所述期望相匹配。

我们可以使用和的组合来实现所需的结果

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

首先,找到从每个列表项中获取要排序的数据的方法:

重新导入
(1)f(-1,--2,--2,-2,-1)=0,”f(-1,--1,--2)=0,”f(-1,--1,--1,--1)=0,”f(-1,--2,--2-2-2)=0,”f(-2,--2,--2,--2,--2,--2,--2,--1)=1,(1,(2,(2,(2)f(-2,(2,--1,--2,(1,--2,-2,-1,-1,(2,-2,-2,-2,(2,-2,(2,-2,-2,-2,(2,-2,-2,-2,(2,(2,-2,(2,-2)0,(2,(2,-2)0,(2,(2,-0,(2,-2,-2)0,(2,(2,-2)0,(2,(0,(2,-2),0,(2)0,(2,(0,(0,(2)0,(2 1(0,1)*((d-2)*p11^2+((2*d+4)*m10^2+(-2*d+4)*m4^2)*p11+(d+2)*“f(-1,1,(1,1)1,(1)1,(1)1)3,(2)10 10 10 10 10扑扑扑扑扑扑扑扑扑扑扑扑扑扑扑扑扑扑灭2 qp计划1211(0,1)1(0,1)*,(p11,(1.1)d*d*1,,,,,,,,,,,,,,,,,,“f(-2*2*2*4,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0,1)*((d-2)*den(2*m10^2)),'f(1,-2)=标度2*u qp_016(1,0)*((d)*p11^2+(-2*d)*m10^2+(2*d+4)*m4^2)*“f(1,(1,-1,-1,,,,“f(1,(1,(1)(1)(1)(1)(1)(1)(1)(1)(1)(1)(1,-1)1)=大规模ar2\U qp-qp-qp计划016(1,0)(1,0)*,(p11-1(1,(1,(1,(1)(1,(1)(1)(1)(1)(1)(10)(1)(10)(10)(10)(10)(10)(10)(1)(10)(10)(10)(10)(10)(10)(10)(10)(10)(10)(10)(10)(10)(10)(2)(2)(2)(2)(2)(2)(2)(2)10)10)10)10)10)10)10)10)10)10)2)2)2)10)10)10)10,(2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,^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)*(2*d)*m4^2,*M44^4^4*4*4*4*4*4*4*3*4*4*4*4*3*4*4*4*4*4*登登(2*4*4 4 4^2*4*4*2*4*2*2*4*2*4*4*2*4*4*3*4*4*4*登登(2*4*4*4^2),2,“,,,“f(2,-,(2,-,(2,-,(1,--1,--1,-1)f,(2,-1)f,(1)2,-1)1)1)1)1=大规模大规模大规模ar2=大规模ar2=大规模ar2(1=大规模ar2(1)大规模ar2(1)大规模ar2(1)大规模ar2(1)大规模ar2(1)大规模ar2)级)大规模ar2(1)准准准准准准准准量量量量量量量量量量量量(((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)*“f(2,2)(2)(2,2)(2)(2)(2)(2)(2)(2)(d-2)*)p11^2+((((d-2)(d-2)d-2)*,(2*d+4+d+4+4十十十十10 10十10十10十10十10 10十2+2+2+2+2+2+2*12*d-16)1,(1,(1,(1)0,(,(2)d-2-2)d-2)d-2)2)*)p11)p11;(2)p11^2)p11^2)p11^2)p11^1^1^1^12*12*12*d-d-d-d-d-d-12*d-d-16*d-16*d-16*d-16-16-16-16-16-16-16-16)4)4,(4)4)4)4)4,(4)4)4)4^4)4,(4)*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']
对于列表1中的项目:
关于findall(r'^f'((-d+),(-d+),(-d+),(项目)]中i的打印([(i[0],i[1]))
这将打印出一个很好的数字对列表,以便
findall
工作。
findall
表达式与正则表达式一样非常简单,因为它所做的只是查找一个序列
f(数字₁,数₂)并返回一个元组
(数字₁,数₂)。这是要排序的数据

这样,您就可以使用元组作为中
参数的输入:

对于排序中的i(列表1,key=lambda项:[(int(i[0]),int(i[1]),对于排序中的i(i[0],i[1]),对于排序中的i.findall(r'f\(((-d+),(-d+)\),项)]:
印刷品(一)
结果是:

f(-2,-2) = 0
f(-2,-1) = 0
f(-2,0) = 0
f(-2,1) = scalar2_qp_1211(0,1)...
f(-2,2) = scalar2_qp_1211(0,1)...
f(-1,-2) = 0
f(-1,-1) = 0
f(-1,0) = 0
f(-1,1) = scalar2_qp_1211(0,1)...
f(-1,2) = scalar2_qp_1211(0,1)...
f(0,-2) = 0
f(0,-1) = 0
f(0,0) = 0
f(0,1) = scalar2_qp_1211(0,1)*...
f(0,2) = scalar2_qp_1211(0,1)*...
f(1,-2) = scalar2_qp_016(1,0)*...
f(1,-1) = scalar2_qp_016(1,0)*...
f(1,0) = scalar2_qp_016(1,0)*(...
f(1,1) = scalar2_qp_1216(1,1)*...
f(1,2) = scalar2_qp_016(1,0)*(...
f(2,-2) = scalar2_qp_016(1,0)*...
f(2,-1) = scalar2_qp_016(1,0)*...
f(2,0) = scalar2_qp_016(1,0)*(...
f(2,1) = scalar2_qp_016(1,0)*(...
f(2,2) = scalar2_qp_016(1,0)*(...

这并没有什么问题,但若您不想要文字(基于字符的)顺序,则必须指定它。@NeAr您让它工作了吗?:)是的,谢谢大家的回答。非常感谢。太棒了。非常感谢。如果将自定义排序键制作成函数,它会更可读,对于初学者来说,
lambda
表达式可能很难理解,只有我的2美分。否则做得很好,看起来我们有相同的想法