排序列表 A2 A4 A1 A3 到 A1 A2 A3 A4

Sort list A2 A4 A1 A3 to A1 A2 A3 A4

我有一个列表,其中包含 A2 A4 A1 A3 B1 B3 B4 A5 B2 等项目。我想将它排序成一个列表,看起来像 A1 A2 A3 A4 A5,然后是 B1 B2 B3 B4 等等。

我已经构建了一个脚本 (ironpython),这可能是朝着正确方向迈出的一步,但我觉得可能有更简单的方法。我的问题是:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
import re
output = []
n = 0
stramienen = IN[0]
gridcurves = IN[1]
var = True
b = []
c = []
e = []
for x in stramienen:
    def hasNumbers(inputString):
        return any(char.isdigit() for char in inputString)
    if (hasNumbers(stramienen[n])) == var:
        b.append(stramienen[n])
        c.append(gridcurves[n])
        e.append(re.findall('\d+',stramienen[n]))
        
    n=n+1
d=zip(b,c,e)
# take second element for sort
def takeSecond(elem):
    return elem[0][0]

# sort list with key
d.sort(key=takeSecond)

#Assign your output to the OUT variable.
OUT = d

sorted()用于对Python

中的列表进行排序
>>> mylist=["B3","A3","A4","B2","A2","B1","A1"]

>>> sorted(mylist)
['A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3']

>>> for i in sorted(mylist): print(i)
... 
A1
A2
A3
A4
B1
B2
B3
>>>

我觉得 python 你需要参加几个 类。例如:

-在for循环

之外定义方法(def myfunction)

-布尔数据的比较是真还是假

def hasNumbers(inputString):
    return any(char.isdigit() for char in inputString)

for x in stramienen:
 if hasNumbers(stramienen[n])==True:
    b.append(stramienen[n])
    c.append(gridcurves[n])
    e.append(re.findall('\d+',stramienen[n]))

希望对您有所帮助。祝你好运。