5

我有一个在 Robot Framework 中使用的嵌套列表。我想在机器人框架级别更改子列表中的一项。

我的列表如下所示:

[鲍勃,玛丽,[六月,七月,八月]]

我想把“七月”改成别的,说“九月”

Robot Framework 会让我更改“bob”或“mary”,但如果我尝试插入一个列表,它会被转换为字符串。

(哦,我试过用“Insert Into List 关键字插入一个新的子列表,和其他 List 关键字,没有任何运气。)

4

3 回答 3

5

我能够使用这样的 Collections 库关键字来实现修改

*** settings ***                                                                       
Library   Collections                                                                

*** test cases ***                                                                     
test    ${l1}=  Create List  1  2  3                                        
        ${l2}=  Create List  foo  bar  ${l1}                                              
        ${sub}=  Get From List  ${l2}  2 
        Set List Value   ${sub}   2   400 
        Set List Value   ${l2}  2  ${sub}  
        Log  ${l2} 

我无法找到直接更改子列表的方法,它必须先被提取,然后修改,最后放回原处。

于 2011-06-08T06:24:40.157 回答
3

我从缺乏回应中猜测,没有一个干净整洁的解决方案。这是我所做的:

因此,我创建了一个实用程序:

class Pybot_Utilities:
    def sublistReplace(self, processList, item, SublistIndex, ItemIndex):
        '''
        Replaces an item in a sublist
        Takes a list, an object, an index to the sublist, and an index to a location in the sublist inserts the object into a sublist of the list at the location specified. 
        So if the list STUFF is (X, Y, (A,B,C)) and you want to change B to FOO give these parameters: [STUFF, FOO, 2, 1]
        '''

        SublistIndex=int(SublistIndex)
        ItemIndex=int(ItemIndex)
        processList[SublistIndex][ItemIndex] = str(item)
        return processList

然后我把这个条目放在我的机器人框架测试套件文件中:

|    | ${ListWithSublist} = | sublistReplace    | ${ListWithSublist]}  | NewItem | 1 | 1 |

(导入我的实用程序库,当然)

运行后,列表索引 1 处的子列表中的第二项(索引 1)将是“NewItem”

也许不是最优雅或最灵活的,但它现在可以完成工作

于 2011-06-03T13:47:26.007 回答
-1

Collections 库中的常规方法“Set List Value”确实适用于嵌入列表 - 并且它就地更改,无需重新创建对象;这是 POC:

${listy}=   Create List     a   b
${inner}=   Create List     1   2
Append To List      ${listy}       ${inner}
Log To Console      ${listy}      # prints "[u'a', u'b', [u'1', u'2']]", as expected

Set List Value      ${listy[2]}    0       4
# ^ changes the 1st element of the embedded list to "4" - both the listy's index (2), and the kw argument (0) can be variables

Log To Console      ${listy}      # prints "[u'a', u'b', [u'4', u'2']]" - i.e. updated
于 2016-09-29T09:42:47.577 回答