如何在python和Robot-framework中切片字典?

时间:2018-12-08 06:21:47

标签: python list dictionary robotframework object-slicing

切片可用于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

如何在pythonRobot-framework中实现字典切片或替代?

3 个答案:

答案 0 :(得分:2)

也许这是您可以考虑的解决方案,因为不能将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'}

答案 1 :(得分:0)

仅使用Robot Framework关键字提供2个替代方法。本质上,它们遵循类似的方法。从字典中获取密钥,然后对其进行切片,然后以所需的格式修改或重新创建字典。

除非有特定原因不想使用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}

答案 2 :(得分:0)

  

我想获取任意3个(可以是随机的)字典元素

不需要构造所有词典项目的列表。您可以使用dict.itemsitertools.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 个项目。