List在編程里面是非常重要的一個數據結構,也有豐富的用法,不過,RF雖然雖然封裝比較好,但和直接用python比起來,靈活性真的是非常差啊
先還是祭出官方文檔 http://robotframework.org/robotframework/latest/libraries/Collections.html
使用前需要導入Collections庫,這個庫在RobotFramework中主要是用來處理列表和字典的
本文呢主要介紹下List的相關用法,字典的處理留到后面再說
1.創建列表 Create List
20190927 15:08:05.612 : INFO : ${list} = ['1', '2', '3']
20190927 15:08:05.612 : INFO : ['1', '2', '3']
2.Append To List
Adds values to the end of list. 向列表的末尾追加元素
20190927 15:17:31.460 : INFO : ${list} = ['1', '2', '3']
20190927 15:17:31.461 : INFO : ['1', '2', '3', 'a', 'b', 'c']
3.Combine Lists 合並兩個列表,並返回合並后的結果
值得注意的是,合並后,原來的兩個列表並沒有改變
20190927 15:20:48.277 : INFO : ${list_1} = ['1', '2', '3']
20190927 15:20:48.277 : INFO : ${list_2} = ['a', 'b', 'c']
20190927 15:20:48.278 : INFO : ${list_3} = ['1', '2', '3', 'a', 'b', 'c']
20190927 15:20:48.279 : INFO : ['1', '2', '3']
20190927 15:20:48.279 : INFO : ['a', 'b', 'c']
20190927 15:20:48.280 : INFO : ['1', '2', '3', 'a', 'b', 'c']
4.Copy List 復制一個列表
20190927 15:25:10.654 : INFO : ${list_1} = ['1', '2', '3']
20190927 15:25:10.655 : INFO : ${list_2} = ['1', '2', '3']
20190927 15:25:10.655 : INFO : ['1', '2', '3']
20190927 15:25:10.656 : INFO : ['1', '2', '3']
5.Get From List 從列表中取值
Index 0 means the first position, 1 the second, and so on. Similarly, -1 is the last position, -2 the second last, and so on.
Using an index that does not exist on the list causes an error. The index can be either an integer or a string that can be converted to an integer.
index為0時,取第一個元素,-1時取最后一個,以此類推。如果index越界,就會報錯
20190927 15:30:49.936 : INFO : ${list} = ['1', '2', '3', 'a', 'b', 'c']
20190927 15:30:49.937 : INFO : ${x} = 1
20190927 15:30:49.937 : INFO : ${y} = 2
20190927 15:30:49.938 : INFO : ${z} = c
5.Get Index From List 返回給定值得索引
20190927 15:33:34.884 : INFO : ${list} = ['1', '2', '3', 'a', 'b', 'c']
20190927 15:33:34.885 : INFO : ${x} = 5
6.Get Slice From List 數組切片
兩個參數(start,end),如果兩個參數都給定了,則返回兩個參數之間的值,就像python里面的 list[start:end]
如果end值缺省,則返回從start開始后面所有的值。start缺省同理
如果值為附屬,則從末尾開始計數
20190927 15:43:08.480 : INFO : ${list} = ['1', '2', '3', 'a', 'b', 'c']
20190927 15:43:08.481 : INFO : ${x} = ['3', 'a', 'b']
20190927 15:43:08.482 : INFO : ${y} = ['3', 'a', 'b', 'c']
20190927 15:43:08.483 : INFO : ${z} = ['b', 'c']
7.Insert Into List 往list中插入數值
20190927 15:46:35.575 : INFO : ${list} = ['1', '2', '3']
20190927 15:46:35.576 : INFO : ['hahah', '1', '2', '3']
20190927 15:46:35.577 : INFO : ['hahah', '1', '2', 'oooo', '3']
8.Remove From List 根據索引移除列表中的值
20190927 15:56:50.199 : INFO : ${list} = ['name1', 'name2', 'name3']
20190927 15:56:50.201 : INFO : ['name1', 'name3']
9.Remove Values From List 根據values移除列表里面的值
20190927 15:58:07.931 : INFO : ${list} = ['name1', 'name2', 'name3']
20190927 15:58:07.933 : INFO : ['name2']
10.Reverse List 倒轉列表
20190927 16:32:09.760 : INFO : ${list} = ['name1', 'name2', 'name3']
20190927 16:32:09.762 : INFO : ['name3', 'name2', 'name1']
11.Set List Value 更新列表中的某一個值
20190927 16:34:16.284 : INFO : ${list} = ['name1', 'name2', 'name3']
20190927 16:34:16.286 : INFO : ['xxx', 'name2', 'name3']
12.Sort List 列表排序
如果是字母則根據字母表排序,如果是數字,則根據數字排序
20190927 16:37:00.375 : INFO : ${list} = ['b', 'a', 'c', '5', '2', '9']
20190927 16:37:00.376 : INFO : ['2', '5', '9', 'a', 'b', 'c']