如何在Robot Framework中合并两个字典?
Robot Framework 合并两个字典为第三个字典
你可以通过以下两种常用方式合并${dictOne}和${dictTwo},得到包含所有键值对的新字典:
方法1:使用Merge Dictionaries关键字
这是Robot Framework自带的标准关键字,直接生成合并后的新字典:
&{dictOne} = Create Dictionary key value anotherkey anothervalue &{dictTwo} = Create Dictionary yetanother_key yetanother_value finalkey finalvalue ${merged_dict}= Merge Dictionaries ${dictOne} ${dictTwo}
方法2:使用Create Dictionary的扩展语法
借助字典展开语法,直接在创建新字典时整合两个字典的内容:
&{dictOne} = Create Dictionary key value anotherkey anothervalue &{dictTwo} = Create Dictionary yetanother_key yetanother_value finalkey finalvalue ${merged_dict}= Create Dictionary &{dictOne} &{dictTwo}
两种方法最终都会得到你期望的结果:
{ 'key': 'value', 'anotherkey': 'anothervalue', 'yetanother_key': 'yetanother_value', 'finalkey': 'finalvalue' }
注意:如果两个字典存在重复键,后面的字典(${dictTwo})中的值会覆盖前面字典(${dictOne})的对应值
内容的提问来源于stack exchange,提问作者Thomas Fritz
相关产品推荐
相关产品推荐

