简体   繁体   English

比较 Robot Framework 中的字典列表

[英]Compare list of dictionary in Robot Framework

I have two list of list of dictionary and i want to compare the vale of first list dictionary to second list of dictionary我有两个字典列表列表,我想将第一个列表字典的值与第二个字典列表进行比较

For example:例如:

Dictionary A contains [{Name:C}, {Name:A}, {Name:B}]
Dictionary B contains [{Name:A}, {Name:B}, {Name:C}]

How to take A's 1st Dictionary {Name:C} and check if it exists in Dictionary B.如何获取 A 的第一个字典 {Name:C} 并检查它是否存在于字典 B 中。

您想查看某些字典列表是否包含至少一个映射'name': 'C'字典?

any(d['name'] == 'C' for d in list_of_dict if 'name' in dict)

If I understand your question correctly, you should be able to do this using the built in Collections library.如果我正确理解您的问题,您应该可以使用内置的 Collections 库来执行此操作。 This code took the values in one dictionary and checked to see if the value exists in the other.此代码获取一个字典中的值并检查该值是否存在于另一个字典中。

*** Settings ***
Library  Collections

*** Variables ***
&{DICTONARY_ONE} =  name1=a  name2=b  name3=c  name4=d
&{DICTONARY_TWO} =  name1=c  name2=a  name3=d  name4=b

*** Test Cases ***
Dictonary Test
    @{dictonary2_list} =  Get Dictionary Values  ${DICTONARY_TWO}
    :For  ${item}  in  @{dictonary2_list}
    \   Dictionary Should Contain Value  ${DICTONARY_ONE}  ${item}

If this is not quite what you are after, there should be something in collections that will let you do what you need.如果这不是你所追求的,那么集合中应该有一些东西可以让你做你需要的。 Here is a link to the documentation.这是文档的链接。
http://robotframework.org/robotframework/latest/libraries/Collections.html http://robotframework.org/robotframework/latest/libraries/Collections.html

I hope that helps.我希望这会有所帮助。

Be it a dictionary or string, as long as it is a part of list it can be compared.无论是字典还是字符串,只要是列表的一部分都可以进行比较。

*** Settings ***
Library  Collections

*** Test Case ***
Dictionary Validation
    ${dict1}=   Create Dictionary   Name    A
    ${dict2}=   Create Dictionary   Name    B
    ${dict3}=   Create Dictionary   Name    C       
    @{li}=  Create List  ${dict3}  ${dict1}  ${dict2}
    @{lj}=  Create List  ${dict1}  ${dict2}  ${dict3}

    :For  ${item1}  in  @{li}
        \   List Should Contain Value  ${lj}  ${item1}  

Just in-case the comparison is on a part of dictionary (not complete dictionary), we need to think of another way.万一比较是在字典的一部分(不是完整的字典)上,我们需要考虑另一种方式。 Let me know if it helps!让我知道它是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM