如何在 python 和 Robot-framework 中分割字典?
How to slice a dictionary in python and Robot-framework?
切片可用于 python
中的列表
list1 =[1,2,3,4,5,6]
list1[:3]
[1, 2, 3]
类似地,切片或类似于字典可用的任何内容?
dict1 = {1":a",2:"b",3:"c",4:"d",5:"e"}
我想获取字典的任意 3 个(可以是随机的)元素,只需提供数字(如上面列表 [:2]
所提供的),那么我应该在字典下方
dict1 = {1":a",2:"b"} # After slicing
如何在 python
和 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'}
仅提供 2 个仅使用 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}
Log Many ${dict1}
Dictionaries Should Be Equal ${dict1} ${result}
${slice} Get Slice From Dictionary ${dict2}
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}=[=10=]
${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}=[=10=]
${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}
I would like to get any 3 (can be random) elements of dictionary
没有必要构建所有字典项目的列表。您可以使用 dict.items
and 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 个项目按插入顺序。
切片可用于 python
中的列表list1 =[1,2,3,4,5,6]
list1[:3]
[1, 2, 3]
类似地,切片或类似于字典可用的任何内容?
dict1 = {1":a",2:"b",3:"c",4:"d",5:"e"}
我想获取字典的任意 3 个(可以是随机的)元素,只需提供数字(如上面列表 [:2]
所提供的),那么我应该在字典下方
dict1 = {1":a",2:"b"} # After slicing
如何在 python
和 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'}
仅提供 2 个仅使用 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}
Log Many ${dict1}
Dictionaries Should Be Equal ${dict1} ${result}
${slice} Get Slice From Dictionary ${dict2}
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}=[=10=]
${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}=[=10=]
${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}
I would like to get any 3 (can be random) elements of dictionary
没有必要构建所有字典项目的列表。您可以使用 dict.items
and 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 个项目按插入顺序。