如何对包含复杂字符串的字母数字列表进行排序

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


f(-2,-2)=..., 

f(-2,-1)=..., 

f(-2,0)=...,

f(-2,1)=...,

f(-2,2)=..., 

..., 

f(2,2)= ...

我试图使用排序的内置函数,但没有帮助。有人可以帮助初学者吗?


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



慕虎7371278
浏览 112回答 4
4回答

沧海一幻觉

我们可以使用正则表达式和自定义排序键的组合来实现所需的结果。import astimport repattern = re.compile('\([+-]?[0-9]*,[+-]?[0-9]*\)')def custom_sort(item):    match = pattern.search(item).group(0)    x, y = ast.literal_eval(match)    return x, ysorted(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))']

繁星点点滴滴

您可以使用 natsort Python 模块根据这些有符号数字的自然排序对此列表进行排序。您可以使用以下命令进行安装:$ pip install natsort(虽然确切的细节可能会有所不同,但你是否正在使用virtualenv,venv,pipenv等。或者,无论您是否在需要额外权限才能在平台上安装模块的平台上,在这种情况下,您可以考虑将其安装在主目录中。搜索Web或StackOverflow,您肯定会找到有关如何为您的设置安装Python模块的帖子。安装后,您可以使用以下命令导入它: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 relist1 = ['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']for item in list1:    print ([(i[0],i[1]) for i in re.findall(r'^f\((-?\d+),(-?\d+)\)', item)])这打印出一个很好的数字对列表,所以这有效。与正则表达式一样,该表达式非常简单,因为它所做的就是在每个列表项中找到一个序列并返回一个元组。这是要排序的数据。findallfindallf(number₁,number₂)(number₁,number₂)这样,您就可以将元组用作 list.sort 中参数的输入:keyfor i in sorted(list1, key=lambda item: [(int(i[0]),int(i[1])) for i in [(i[0],i[1]) for i in re.findall(r'f\((-?\d+),(-?\d+)\)', item)]]):    print (i)结果如下:f(-2,-2) = 0f(-2,-1) = 0f(-2,0) = 0f(-2,1) = scalar2_qp_1211(0,1)...f(-2,2) = scalar2_qp_1211(0,1)...f(-1,-2) = 0f(-1,-1) = 0f(-1,0) = 0f(-1,1) = scalar2_qp_1211(0,1)...f(-1,2) = scalar2_qp_1211(0,1)...f(0,-2) = 0f(0,-1) = 0f(0,0) = 0f(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)*(...

胡子哥哥

如果你想使用内置的,我认为应该可以根据你想要应用的排序逻辑将你的字符串拆分成元组。sort下面是一个通用示例:a = [("c", 2), ("a", 2), ("a", 1), ("b", 4)] print(sorted(a))这打印:[('a', 1), ('a', 2), ('b', 4), ('c', 2)]在您的情况下,您可以拆分为 ,依此类推,具体取决于您的条目的复杂程度。"f(-2,1)"["f(", -2, ",", 1, ")"]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python