亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何對包含復雜字符串的字母數字列表進行排序

如何對包含復雜字符串的字母數字列表進行排序

慕虎7371278 2022-08-16 18:48:53
我需要你的幫助,因為我陷入了這個問題。我想對一個由丑陋的字符串和整數組成的列表進行排序。我想要的順序如下:從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']
查看完整描述

4 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

我們可以使用正則表達式自定義排序鍵的組合來實現所需的結果。

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



查看完整回答
反對 回復 2022-08-16
?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

您可以使用 natsort Python 模塊根據這些有符號數字的自然排序對此列表進行排序。


您可以使用以下命令進行安裝:


$ pip install natsort

(雖然確切的細節可能會有所不同,但你是否正在使用virtualenv,venv,pipenv等?;蛘撸瑹o論您是否在需要額外權限才能在平臺上安裝模塊的平臺上,在這種情況下,您可以考慮將其安裝在主目錄中。搜索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))']

您可以輕松地將排序順序與您聲明的期望相匹配。


查看完整回答
反對 回復 2022-08-16
?
森林海

TA貢獻2011條經驗 獲得超2個贊

首先找到一種方法來獲取要從每個列表項中排序的數據:


import re

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


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 中參數的輸入:key


for 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) = 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)*(...


查看完整回答
反對 回復 2022-08-16
?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

如果你想使用內置的,我認為應該可以根據你想要應用的排序邏輯將你的字符串拆分成元組。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, ")"]


查看完整回答
反對 回復 2022-08-16
  • 4 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號