環/課程/列表
在本節中,我們將學習如何處理列表。
我們可以透過在方括號內定義列表專案來建立新的列表。
示例
aList = [1,2,3,4,5]我們也可以使用 : 運算子建立新的列表
示例
aList = 1:5
aList2 = "a":"z"我們也可以使用 list() 函式建立列表
語法
list = list(size)示例
aList = list(10) # aList contains 10 items.. 注意:列表索引從 1 開始
要向列表新增新專案,我們可以使用 Add() 函式。
語法
Add(List,Item)
示例
aList = ["one","two"]
add(aList,"three")我們也可以使用 + 運算子來實現。
語法
List + item示例
aList = 1:10 # create list contains numbers from 1 to 10
aList + 11 # add number 11 to the list
see aList # print the list我們可以使用 len() 函式獲取列表大小
語法
Len(List)示例
aList = 1:20 see len(aList) # print 20要從列表中刪除專案,我們可以使用 del() 函式
語法
del(list,index)示例
aList = ["one","two","other","three"]
Del(aList,3) # delete item number three
see aList # print one two three要從列表中獲取專案,我們使用以下語法
List[Index]示例
aList = ["Cairo","Riyadh"]
see "Egypt : " + aList[1] + nl +
"KSA : " + aList[2] + nl要設定列表中專案的 value,我們可以使用以下語法
List[Index] = Expression示例
aList = list(3) # create list contains three items
aList[1] = "one" aList[2] = "two" aList[3] = "three"
see aList要查詢列表中的專案,我們可以使用 find() 函式
語法
Find(List,ItemValue) ---> Item Index
Find(List,ItemValue,nColumn) ---> Search in nColumn, returns the Item Index示例
aList = ["one","two","three","four","five"]
see find(aList,"three") # print 3示例
mylist = [["one",1],
["two",2],
["three",3]]
see find(mylist,"two",1) + nl # print 2
see find(mylist,2,2) + nl # print 2我們也可以使用 binarysearch() 函式在排序後的列表中進行搜尋。
語法
BinarySearch(List,ItemValue) ---> Item Index
BinarySearch(List,ItemValue,nColumn) ---> Search in nColumn, returns the Item Index示例
aList = ["one","two","three","four","five"]
aList = sort(aList)
see binarysearch(aList,"three")輸出
five
four
one
three
two
4我們可以使用 sort() 函式對列表進行排序。
語法
Sort(List) ---> Sorted List
Sort(List,nColumn) ---> Sorted List based on nColumn示例
aList = [10,12,3,5,31,15]
aList = sort(aList) see aList # print 3 5 10 12 15 31我們可以對字串列表進行排序
示例
mylist = ["mahmoud","samir","ahmed","ibrahim","mohammed"]
see mylist # print list before sorting
mylist = sort(mylist) # sort list
see "list after sort"+nl
see mylist # print ahmed ibrahim mahmoud mohammed samir我們可以根據特定列對列表進行排序。
示例
aList = [ ["mahmoud",15000] ,
["ahmed", 14000 ] ,
["samir", 16000 ] ,
["mohammed", 12000 ] ,
["ibrahim",11000 ] ]
aList2 = sort(aList,1)
see aList2輸出
ahmed
14000
ibrahim
11000
mahmoud
15000
mohammed
12000
samir
16000我們可以使用 reverse() 函式反轉列表。
語法
Reverse(List) ---> Reversed List示例
aList = [10,20,30,40,50]
aList = reverse(aList)
see aList # print 50 40 30 20 10要將專案插入列表中,我們可以使用 insert() 函式。
語法
Insert(List,Index,Item)插入的專案將位於索引之後
示例
aList = [1,2,4,5]
insert(aList,2,3)
see aList # print 1 2 3 4 5列表可能包含其他列表
示例
aList = [ 1 , [10,20,30] , 5 , [100,1000,5000] ]
aList2 = [
"one","two",
[3,4],
[20,30], ["three",
"four",
"five",[100,200,300]
]
]
see aList[2] # print 10 20 30
see aList[4][3] + nl # print 5000
see aList2[5][2] + nl # print four
see aList2[5][4][3] # print 300我們可以使用賦值運算子複製列表(包括巢狀列表)。
示例
aList = [
"one","two",
[3,4],
[20,30], ["three",
"four",
"five",[100,200,300]
]
]
aList2 = aList # Copy aList to aList2
aList2[5] = "other" # modify item number five
see aList2[5] + nl # print other
see aList[5] # print three four five 100 200 300列表是 `一等公民 <http://en.wikipedia.org/wiki/First-class_citizen>`_,我們可以將列表儲存在變數中,將列表傳遞給函式,以及從函式中返回列表。
示例
aList = duplicate( [1,2,3,4,5] )
see aList[10] + nl # print 5
see mylist() # print 10 20 30 40 50
func duplicate list
nMax = len(list)
for x = 1 to nMax
list + list[x]
next
return list
func mylist return [10,20,30,40,50]我們可以在第一次定義列表時使用列表專案。
示例
aList = [ [1,2,3,4,5] , aList[1] , aList[1] ]
see aList # print 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
列表是透過引用傳遞給函式的。這意味著被呼叫函式將在同一個列表上工作,並且可以修改它。
示例
func main
aList = [1,2,3,4,5] # create list, local in function main
myfunc(aList) # call function, pass list by reference
see aList # print 1 2 3 4 5 6 7 8 9 10
func myfunc list
list + [6,7,8,9,10]在獲取專案 value 或設定專案 value 時,我們可以使用字串索引訪問專案,而不是使用數字來確定專案索引,如果專案是一個包含兩個專案的列表,並且第一個專案是字串。
示例
aList = [ ["one",1] , ["two",2] , ["three",3] ]
see aList["one"] + nl +
aList["two"] + nl +
aList["three"] # print 1 2 3這種型別的列表可以使用 : 和 = 運算子以更簡潔的語法定義。
示例
aList = [ :one = 1 , :two = 2 , :three = 3 ]
see aList["one"] + nl +
aList["two"] + nl +
aList["three"] + nl # print 1 2 3
see aList[1] # print one 1.. 提示:在識別符號(一個詞)之前使用 : 表示字面量
.. 注意:在列表定義中使用 = 會建立一個包含兩個專案的列表,其中第一個專案是左側,第二個專案是右側。
我們可以使用字串索引向列表新增新專案
示例
aList = []
aList["Egypt"] = "Cairo"
aList["KSA"] = "Riyadh"
see aList["Egypt"] + nl + # print Cairo
aList["KSA"] + nl # print Riyadh這種型別的列表非常適合將引數傳遞給函式,因為引數的順序並不重要(我們可以更改順序)。
此外,一些引數可能是可選的。
示例
myconnect ( [ :server = "myserver.com" , :port = 80 ,
:username = "mahmoud" , :password = "password" ] )
func myconnect mypara
# print connection details
see "User Name : " + mypara[:username] + nl +
"Password : " + mypara[:password] + nl +
"Server : " + mypara[:server] + nl +
"Port : " + mypara[:port]