写在最前
注:由于单版面展示过多会显得比较杂乱,因此拆分成几个篇章,之后会单做一个目录。
这是由Mono开展的第二个长期项目,会将Sinet库中相关数据类型独立出来,通过解析和代码示例介绍数据类型的用法。通过巧妙运用数据类型可以简化工作流程。
***引流:请喜欢Python的或是对于人工智能感兴趣的程序员关注我的另一个(大概)每周更新项目《Sinet库》,目前已经迁移了包括Numpy、Pandas和Sklearn在内的大量Python相关函数内容。
什么是List类型
简单来说,本质上他是array类型将索引起始值更改为0。但在继承的同时,我赋予了这个类型更多的自带属性函数,以及切片访问的办法。
注意事项
该数据类型依赖的数据类型:无
该数据类型依赖函数:ListCmp、ToString
自带属性函数功能解析及范例
1.枚举
在For in体系中,如果不重写枚举办法,就会使得双参数枚举时第一个枚举出的索引从1开始,与预期不符。
用法示例:(Print为调试函数)
tmp := List(1, 2, 3, 4)
tmp2 := List()
For i, j in tmp
tmp2.append([i, j])
print(tmp2)
2.切片访问
这一点是python的列表和ahk的array最大的不同。尽管都不支持多维访问,但如果可以通过切片访问多个元素,那么对于列表的操作和处理上也事半功倍。当然如果想要对多维数组进行操作的话,或许会讲解Numahk这个数据类型(对应Sinet库里的Numpy类型)
用法示例:
tmp := List(1, 2, 3, 4)
tmp2 := tmp["1:"]
print(tmp2)
; Output => [2,3,4]
3.Append
与array类型的push一致
4.Count
这个函数用于获取元素在列表中出现次数,为了优化匹配速度,我采用的是转字符串比较,具体内容见依赖函数ListCmp的代码。
用法示例:
tmp := List([1,2,3],[1,2,3],[2,3])
print(tmp.count([1,2,3]))
; Output => 2
待续
完整代码
Class List Extends Array
{
NextIdx := 0
__Enum(Number)
{
Index := this.Length
Return Fn
Fn(&idx := 0, &value := 0)
{
if IsSet(value)
{
Try
{
idx := this[this.Length - Index]
Return Index-- >= 0
}
Catch
Return 0
}
else
{
Try
{
idx := this.Length - Index
value := this[this.Length - Index]
Return Index-- >= 0
}
Catch
Return 0
}
}
}
__Item[Index]
{
Get => this.GetMethod(Index)
Set => this.SetMethod(Index, Value)
}
Append(Object*)
{
Loop Object.Length
this.Push(Object[A_Index])
}
Count(Object)
{
Return_Count := 0
For i in this
{
if !ListCmp(i, Object)
Return_Count++
}
Return Return_Count
}
Extend(Extend_Lst*)
{
Loop Extend_Lst.Length
{
if Type(Extend_Lst[A_Index]) !== "Array" && Type(Extend_Lst[A_Index]) !== "List"
this.Push(Extend_Lst[A_Index])
else
{
For i in Extend_Lst[A_Index]
this.Push(i)
}
}
}
GetMethod(Index)
{
Try
Index := Integer(Index)
if Type(Index) == "Integer"
Return Super[this.Init(Index)]
else if InStr(Index, ":")
{
Lst_Res := List()
Lst_Index := StrSplit(Index, ":")
if !Trim(Lst_Index[1])
Lst_Index[1] := 0
if !Trim(Lst_Index[2])
Lst_Index[2] := this.Length
For i in XRange(Trim(Lst_Index[1]), Trim(Lst_Index[2]))
Lst_Res.Append(Super[this.Init(i)])
Return Lst_Res
}
}
Index(Object)
{
Flag := 0
Index := 0
For i in this
{
if !Sinet.List.Cmp(i, Object)
{
Flag := 1
Break
}
Index++
}
if !Flag
Index := -1
Return Index
}
Init(Index)
{
if Index >= 0
Index += 1
Return Index
}
Insert(Index, Object*)
{
this.InsertAt(this.Init(Index), Object*)
}
List(Lst*)
{
Return this.Plus(Lst*)
}
ListMap(Map)
{
Lst_Map := List()
if Type(Map) !== "Map" && Type(Map) !== "Dict"
Return "Illegal Input"
else
{
For i, Value in Map
Lst_Map.Append([i, Value])
Return Lst_Map
}
}
Max()
{
Return Max(this*)
}
Min()
{
Return Min(this*)
}
Multiple(Number)
{
Lst := this.Clone()
Loop Number - 1
{
For i in Lst
{
Try
{
this.Append(i.Clone())
Continue
}
Catch
Error := 1
this.Append(i)
}
}
}
Next()
{
if this.NextIdx <= 0
this.NextIdx := this.Length
Res := this[this.Length - this.NextIdx]
this.NextIdx--
Return Res
}
Plus(Lst*)
{
Loop Lst.Length
{
if Type(Lst[A_Index]) !== "Array" && Type(Lst[A_Index]) !== "List"
this.Append(Lst[A_Index])
else
{
For i in Lst[A_Index]
this.Append(i)
}
}
}
Pop(Index := -1)
{
if Index == -1 || Index >= this.Length
Return this.RemoveAt(-1)
else
Return this.RemoveAt(this.Init(Index))
}
Remove(Object*)
{
Loop Object.Length
{
Index := this.Index(Object[A_Index])
if Index != -1
this.Pop(Index)
}
}
Reverse()
{
Loop this.Length // 2
Sinet.Swap(A_Index - 1, this.Length - A_Index, this)
}
SetMethod(Index, Value)
{
Try
Index := Integer(Index)
if Type(Index) == "Integer"
Super[this.Init(Index)] := Value
else if InStr(Index, ":")
{
Lst_Index := StrSplit(Index, ":")
if !Trim(Lst_Index[1])
Lst_Index[1] := 0
if !Trim(Lst_Index[2])
Lst_Index[2] := this.Length
For i in XRange(Trim(Lst_Index[1]), Trim(Lst_Index[2]))
Super[this.Init(i)] := Value
}
}
SortMap(Map)
{
if Type(Map) !== "Map" && Type(Map) !== "Dict"
Return "Illegal Input"
Lst_Map := this.ListMap(Map)
For i in XRange(0, Lst_Map.Length)
{
For j in XRange(0, Lst_Map.Length)
{
if Lst_Map[i][1] > Lst_Map[j][1]
Swap(i, j, Lst_Map)
}
}
Return Lst_Map
}
Title()
{
Loop this.Length
if Type(this[A_Index - 1]) == "String"
this[A_Index - 1] := StrTitle(this[A_Index - 1])
}
}
依赖函数代码
ListCmp
ListCmp(Lst1, Lst2)
{
Lst1 := ToString(Lst1)
Lst2 := ToString(Lst2)
if StrCompare(Lst1, Lst2) < 0
Return -1
else if StrCompare(Lst1, Lst2) > 0
Return 1
else
Return 0
}
ToString
ToString(Text)
{
if Type(Text) == "Array"
{
if Text.Length < 1
Text.InsertAt(1, "")
String_Plus := ""
String_Text := "[" . ToString(Text[1])
Loop Text.Length - 1
String_Plus .= "," . ToString(Text[A_Index + 1])
String_Text .= String_Plus
String_Text .= "]"
Return String_Text
}
else if Type(Text) == "List"
{
if Text.Length < 1
Text.InsertAt(1, "")
String_Plus := ""
String_Text := "[" . ToString(Text[0])
Loop Text.Length - 1
String_Plus .= "," . ToString(Text[A_Index])
String_Text .= String_Plus
String_Text .= "]"
Return String_Text
}
else if Type(Text) == "ComObjArray"
{
if Text.MaxIndex() < 0
{
Text := ComObjArray(VT_VARIANT:=12, 1)
Text[0] := ""
}
String_Plus := ""
String_Text := "[" . ToString(Text[0])
Loop Text.MaxIndex()
String_Plus .= "," . ToString(Text[A_Index])
String_Text .= String_Plus
String_Text .= "]"
Return String_Text
}
else if Type(Text) == "Numpy.NDArray"
{
if Text.array.Length < 1
Text.array.InsertAt(1, "")
String_Plus := ""
String_Text := "[" . ToString(Text.array[0])
Loop Text.array.Length - 1
String_Plus .= "," . ToString(Text.array[A_Index])
String_Text .= String_Plus
String_Text .= "]"
Return String_Text
}
else if Type(Text) == "Set"
Return "Set(" ToString(Text.list) ")"
else if Type(Text) == "Map" || Type(Text) == "Dict"
{
String_Text := "{"
For i, Value in Text
String_Text .= ToString(i) . ":" . ToString(Value) . ","
if SubStr(String_Text, -1) !== "{"
String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
String_Text .= "}"
Return String_Text
}
else if Type(Text) == "Pandas.DataFrame"
{
String_Text := "`t"
For i in Text.Columns
String_Text .= ToString(i) "`t"
String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
String_Text .= "`n"
Loop Text.Index.Length
{
index := A_Index - 1
String_Text .= ToString(Text.Index[index]) "`t"
Loop Text.Data.array.Length
String_Text .= ToString(Text.Data.array[A_Index - 1][index]) "`t"
String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
String_Text .= "`n"
}
String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
Return String_Text
}
else if Type(Text) == "Integer" || Type(Text) == "Float" || Type(Text) == "String"
Return String(Text)
else if Type(Text) == "Object"
{
String_Text := "{"
For i, Value in Text.OwnProps()
String_Text .= ToString(i) . ":" . ToString(Value) . ","
if SubStr(String_Text, -1) !== "{"
String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
String_Text .= "}"
Return String_Text
}
else if Type(Text) == "ListNode"
{
String_Text := "{val:"
String_Text .= ToString(Text.val)
String_Text .= "}"
if Text.next
{
String_Text .= "->"
String_Text .= ToString(Text.next)
}
Return String_Text
}
else
Return "#Null#"
}
调试函数Print
Print(Text*)
{
Global Print_Gui
Global Print_Edit
Print_Text := ""
Loop Text.Length
{
String_Text := ToStringPrint(Text[A_Index])
if SubStr(String_Text, -1) == "," && (Type(Text[A_Index]) == "Array" || Type(Text[A_Index]) == "List" || Type(Text[A_Index]) == "ComObjArray" || Type(Text[A_Index]) == "Numpy.NDArray" || Type(Text[A_Index]) == "Map" || Type(Text[A_Index]) == "Dict" || Type(Text[A_Index]) == "Object")
String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
Print_Text .= String_Text "`n"
}
Print_Text := SubStr(Print_Text, 1, StrLen(Print_Text) - 1)
if WinExist("Sinet Print")
{
WinActivate("Sinet Print")
Print_Edit.Value .= "`n" Print_Text
Return
}
Print_Gui := Gui()
Print_Gui.Title := "Sinet Print"
Print_Gui.BackColor := "87CEFA"
Print_Edit := Print_Gui.Add("Edit", "R30 W800 ReadOnly")
Print_Edit.SetFont("S12", "Arial")
Print_Edit.Value := Print_Text
Print_Gui.Show()
}
调试函数依赖ToStringPrint
ToStringPrint(Text)
{
if Type(Text) == "Array"
{
if Text.Length < 1
Text.InsertAt(1, "")
String_Plus := ""
String_Text := "[" . ToStringPrint(Text[1])
Loop Text.Length - 1
String_Plus .= "," . ToStringPrint(Text[A_Index + 1])
String_Text .= String_Plus
String_Text .= "]"
Return String_Text
}
else if Type(Text) == "List"
{
if Text.Length < 1
Text.InsertAt(1, "")
String_Plus := ""
String_Text := "[" . ToStringPrint(Text[0])
Loop Text.Length - 1
String_Plus .= "," . ToStringPrint(Text[A_Index])
String_Text .= String_Plus
String_Text .= "]"
Return String_Text
}
else if Type(Text) == "ComObjArray"
{
if Text.MaxIndex() < 0
{
Text := ComObjArray(VT_VARIANT:=12, 1)
Text[0] := ""
}
String_Plus := ""
String_Text := "[" . ToStringPrint(Text[0])
Loop Text.MaxIndex()
String_Plus .= "," . ToStringPrint(Text[A_Index])
String_Text .= String_Plus
String_Text .= "]"
Return String_Text
}
else if Type(Text) == "Numpy.NDArray"
{
if Text.array.Length < 1
Text.array.InsertAt(1, "")
String_Plus := ""
String_Text := "[" . ToStringPrint(Text.array[0])
Loop Text.array.Length - 1
String_Plus .= "," . ToStringPrint(Text.array[A_Index])
String_Text .= String_Plus
String_Text .= "]"
Return String_Text
}
else if Type(Text) == "Set"
Return "Set(" ToStringPrint(Text.list) ")"
else if Type(Text) == "Map" || Type(Text) == "Dict"
{
String_Text := "{"
For i, Value in Text
String_Text .= ToStringPrint(i) . ":" . ToStringPrint(Value) . ","
if SubStr(String_Text, -1) !== "{"
String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
String_Text .= "}"
Return String_Text
}
else if Type(Text) == "Pandas.DataFrame"
{
String_Text := " `t"
For i in Text.Columns
String_Text .= ToStringPrint(i) "`t"
String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
String_Text .= "`n"
Loop Text.Index.Length
{
index := A_Index - 1
String_Text .= ToStringPrint(Text.Index[index]) "`t"
Loop Text.Data.array.Length
String_Text .= ToStringPrint(Text.Data.array[A_Index - 1][index]) "`t"
String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
String_Text .= "`n"
}
Return String_Text
}
else if Type(Text) == "Integer"
Return Text
else if Type(Text) == "Float"
{
Sinet.NTLCalc.SetOutputPrecision(8)
Sign := Text < 0
Text := Sinet.NTLCalc(Abs(Text))
if Text - Integer(Text) == 0
Return Integer(Text)
Loop 10 - StrLen(Text)
Text .= "0"
if Sign
Text := "-" Text
Return Text
}
else if Type(Text) == "String"
{
if isInteger(Text)
Return Integer(Text)
if isFloat(Text)
{
Sinet.NTLCalc.SetOutputPrecision(8)
Sign := Text < 0
Text := Sinet.NTLCalc(Abs(Text))
if Text - Integer(Text) == 0
Return Integer(Text)
Loop 10 - StrLen(Text)
Text .= "0"
if Sign
Text := "-" Text
Return Text
}
Lst_Text := StrSplit(Text, "")
Try
{
if Lst_Text[1] !== '"' || Lst_Text[-1] !== '"'
Return '"' . Text . '"'
else
Return Text
}
Catch
Return Text
}
else if Type(Text) == "Object"
{
String_Text := "{"
For i, Value in Text.OwnProps()
String_Text .= ToStringPrint(i) . ":" . ToStringPrint(Value) . ","
if SubStr(String_Text, -1) !== "{"
String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
String_Text .= "},"
Return String_Text
}
else if Type(Text) == "ListNode"
{
String_Text := "{val:"
String_Text .= ToStringPrint(Text.val)
String_Text .= "}"
if Text.next
{
String_Text .= "->"
String_Text .= ToStringPrint(Text.next)
}
Return String_Text
}
else
Return "#Type: " Type(Text) "#"
}