【问题标题】:How to slice a dictionary in python and Robot-framework?如何在 python 和 Robot-framework 中对字典进行切片?
【发布时间】:2018-12-08 06:21:46
【问题描述】:

切片可用于 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 中实现此字典切片或替代?

【问题讨论】:

  • Slicing a dictionary的可能重复
  • @b-fg 你如何在机器人框架中切片它?这是重复的吗?
  • 在 python 中,你如何通过提供 list 中的大小来限制字典大小(切片)?我在你提供的链接中没有看到。
  • 请记住,在 Python 2 和 pre-3.6 中,字典是无序集合,切片在每次运行时会返回不同的结果。

标签: python list dictionary robotframework object-slicing


【解决方案1】:

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

【讨论】:

  • 非常感谢。确实是一个简单的最佳解决方案。编辑上面的答案,因为它返回“TypeError: 'dict_items' object is not subscriptable”([low:high] 需要在列表上操作)
【解决方案2】:

仅使用 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}

【讨论】:

    【解决方案3】:

    我想获取字典的任意 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 个项目。

    【讨论】:

      猜你喜欢
      • 2018-08-14
      • 1970-01-01
      • 2017-03-19
      • 2018-11-28
      • 2021-11-19
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多