我有两个问题:
1)是否有更有效的方法来创建如下的字母数字列表?
def CreateList():
numb=range(1,21)
StringList=[]
for i in numb:
StringList.append("C"+str(i))
return StringList
StringList=CreateList()
print (StringList)
['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10','C11','C12 ','C13','C14','C15','C16','C17','C18','C19','C20']
2)如果我有一个如下所示的字母数字列表,那么排序它的最佳方法是什么?
['C1','C10','C11','C12','C13','C14','C15','C16','C17','C18','C19','C2 ','C20','C3','C4','C5','C6','C7','C8','C9']
答案 0 :(得分:0)
Sub TestMethod()
Dim Lne As String
Dim i As Integer
Dim aryTextFile() As String
Dim newfile() As String
Dim count As Integer
Line = "C1,C10,C11,C12,C13,C14,C15,C16,C17,C18,C19,C2,C20,C3,C4,C5,C6,C7,C8,C9"
aryTextFile = Split(Line, ",")
ReDim Preserve newfile(UBound(aryTextFile)) As String
count = 0
For i = 0 To UBound(aryTextFile)
If Len(aryTextFile(i)) <= 2 Then
newfile(count) = aryTextFile(i)
count = count + 1
End If
Next i
For i = 0 To UBound(aryTextFile)
If Len(aryTextFile(i)) = 3 Then
newfile(count) = aryTextFile(i)
count = count + 1
End If
Next i
For i = 0 To UBound(aryTextFile)
If Len(aryTextFile(i)) = 4 Then
newfile(count) = aryTextFile(i)
count = count + 1
End If
Next i
newLine = Join(newfile, ",")
strOut = newLine
Debug.Print (strOut)
End Sub
我有类似的事情。如果列表按字母数字顺序排列,则可以使用for循环加载新数组,以在原始排序数组中查找字符串长度为2的字符串,并将其加载到新数组的开头。然后是另一个for循环,其字符串长度为3,依此类推。