Collections库使用

时间:2019-11-22
本文章向大家介绍Collections库使用,主要包括Collections库使用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
*** Settings ***
Library Collections
*** Test Cases ***
list
#  定义列表
${list1} create list a b c  
${list2} create list 1 2 3
#  添加元素到列表中
append to list ${list1} d
log to console ${list1} # ['a', 'b', 'c', 'd']
#  列表与列表结合,返回值为新的列表
${list3} combine lists ${list1} ${list2}
log to console ${list3} #['a', 'b', 'c', 'd', '1', '2', '3']
${list6} evaluate [1,2,3,3,5,2,1,6,3]
${list4} convert to list ${list6}
log to console ${list4} #[1, 2, 3]
${list7} evaluate [1,[3,4],6,7]
#  赋值列表(深拷贝与浅拷贝)
${list8} copy list ${list7} deepcopy=True
log to console ${list8}
#   计算元素在列表中的次数 相当于python中的count()函数
${count} count values in list ${list6} ${3} start=0
log to console ${count} # 3
${list9} create list a b c d
#  判断两个列表是否相等
lists should be equal ${list1} ${list9}

${var} get from list ${list1} 1
log to console ${var} # 返回下标为1的元素 b

${list11} create list a b c d a a1a
${index} get index from list ${list11} a start=1 end=None
log to console ${index} # 返回元素下标(找到即返回),可以指定寻找范围

${var1} get match count ${list11} a*
log to console ${var1} #匹配列表中出现a的次数

insert into list ${list11} 1 ff
log to console ${list11} # 指定index插入值到列表中['a', 'ff', 'b', 'c', 'd', 'a', 'a1a']

remove values from list ${list11} ff a1a
log to console ${list11} # 从列表中移除元素 ['a', 'b', 'c', 'd', 'a']

remove from list ${list11} 0
log to console ${list11} # 根据index从列表中移除元素['b', 'c', 'd', 'a']

set list value ${list11} 0 gg
log to console ${list11} # 根据index 修改元素['gg', 'c', 'd', 'a']

sort list ${list11}
log to console ${list11} # ['a', 'c', 'd', 'gg']

dictionary
# 创建字典
${dict1} create dictionary a=1 b=2 c=3
${dict2} convert to dictionary ${dict1}

log to console ${dict1}
${dict3} evaluate {'a':[1,2,3],'b':{'x':1,'y':2}}
#  判断两个字典是否相等
dictionaries should be equal ${dict1} ${dict2}
#  判断字典中是否包含a=1
dictionary should contain item ${dict1} a 1
# 判断是否包含key a
dictionary should contain key ${dict1} a
#  判断是否包含value值为1
dictionary should contain value ${dict1} 1
${dict4} evaluate {'b':{'x':1,'y':2}}

dictionary should contain sub dictionary ${dict3} ${dict4} # 第一个参数包含第二个参数
#  获取字典的items ,返回值为列表
${items} get dictionary items ${dict1} sort_keys=False
log to console ${items} #['a', '1', 'b', '2', 'c', '3']
#  获取字典中所有key
${keys} get dictionary keys ${dict1} sort_keys=False
log to console ${keys} #['a', 'b', 'c']
#  获取字典中所有value
${values} get dictionary values ${dict1}
log to console ${values} #['1', '2', '3']
#  根据key获取value
${value} get from dictionary ${dict1} a
log to console ${value} # 返回key为a的value 1

原文地址:https://www.cnblogs.com/aiyumo/p/11912044.html