切片可用于python 中的列表
list1 =[1,2,3,4,5,6]
list1[:3]
[1, 2, 3]
类似地,切片或者类似于dictionary的东西?
dict1 = {1":a",2:"b",3:"c",4:"d",5:"e"}
我想得到字典的任何3个(可以是随机的(元素,只需提供数字(如上面为列表[:2]
提供的(,那么我应该得到字典以下的元素
dict1 = {1":a",2:"b"} # After slicing
如何在CCD_ 2&Robot-framework
?
也许这是一个您可以考虑的解决方案,因为dict
不能作为list
:访问
dict1 = {1:"a",2:"b",3:"c",4:"d",5:"e"}
def take(dct, high=None, low=None):
return dict(list(dct.items())[low:high])
print(take(dict1, 3)) #=> {1: 'a', 2: 'b', 3: 'c'}
print(take(dict1, 5, 2)) #=> {3: 'c', 4: 'd', 5: 'e'}
仅使用Robot Framework关键字提供两个备选方案。从本质上讲,他们遵循类似的方法。从字典中获取密钥,然后对其进行切片,然后以所需格式修改或重新创建字典。
除非有特定的原因不想使用Python,否则我认为这个功能应该由Python关键字而不是Robot Framework提供。
*** Settings ***
Library Collections
*** Variables ***
&{dict1} 1=a 2=b 3=c 4=d 5=e
&{dict2} 1=a 2=b 3=c 4=d 5=e
&{result} 3=c 4=d 5=e
*** Test Cases ***
TC - keep items 3, 4 & 5
# Keey
Keep Slice In Dictionary ${dict1} ${5} ${2}
Log Many ${dict1}
Dictionaries Should Be Equal ${dict1} ${result}
${slice} Get Slice From Dictionary ${dict2} ${5} ${2}
Log Many ${slice}
Dictionaries Should Be Equal ${dict1} ${slice}
*** Keywords ***
Keep Slice In Dictionary
[Documentation]
... Modifies the dictionary to only leave the slice.
...
... The keys of the dictionary are converted into a list. Then
... this list is spliced. This list is then used to filter out
... the unwanted keys.
...
... Note: this keyword modifies the provided dictionary.
...
... Arguments:
... - dict (dictionary) The dictionary that needs to be modified
... - high (integer) The last item to be kept.
... - low (integer) The first item of the slice. (defaults to 0)
...
... Returns: None Modifies the provided dictionary.
...
[Arguments] ${dict} ${high} ${low}=${0}
${keys_list} Get Dictionary Keys ${dict}
${filtered_keys} Get Slice From List ${keys_list} ${low} ${high}
Keep In Dictionary ${dict} @{filtered_keys}
Get Slice From Dictionary
[Documentation]
... Get a slice of sequential keys from a dictionary
...
... The keys of the dictionary are converted into a list. Then
... this list is spliced. This list is then used to create a new
... Dictionary with the filtered keys.
...
... Arguments:
... - dict (dictionary) The source dictionary
... - high (integer) The last item to be kept.
... - low (integer) The first item of the slice. (defaults to 0)
...
... Returns: (dictionary A dictionary with the desired keys.
...
[Arguments] ${dict} ${high} ${low}=${0}
${keys_list} Get Dictionary Keys ${dict}
${filtered_keys} Get Slice From List ${keys_list} ${low} ${high}
${return_dict} Create Dictionary
:FOR ${item} IN @{filtered_keys}
Set To Dictionary ${return_dict} ${item} ${dict['${item}']}
[Return] ${return_dict}
我想获得字典的任意3个元素(可以是随机的(
不需要构造所有字典项的列表。您可以使用dict.items
和itertools.islice
对固定数量的项目进行切片:
from itertools import islice
def get_n_items(d, n):
return dict(islice(d.items(), 0, n))
dict1 = {1:"a", 2:"b", 3:"c", 4:"d", 5:"e"}
get_n_items(dict1, 2) # {1: 'a', 2: 'b'}
get_n_items(dict1, 3) # {1: 'a', 2: 'b', 3: 'c'}
对于Python 3.6+,作为CPython 3.6和3.7+中的一个实现细节,这相当于按插入顺序获取第一个n项。