如何将列表的元素传递给变量

时间:2016-11-28 11:51:02

标签: python python-2.7 list variables subprocess

def delete_events(self):


    self.ucn = self.user_channel_number
    print 'The channel number in the process: ', self.ucn

    self.bids = self.channel_events_book_ids
    print 'Events book ids', self.bids
    print '', len(self.bids), 'events on the planner will be deleted'

    are_you_sure = raw_input('Channel number is correct. Are you sure to delete channel number? (y/n): ') 

    if are_you_sure == 'y' and len(self.bids) !=0 :

        print 'The selected program will be deleted'

        action = 'DeleteEvent'
        menu_action = 'all'
        book = self.bids[0]
        arg_list = [('C:\\Users\\yke01\\Documents\\StormTest\\Scripts\\Completed'
                        '\\Utils\\UPNP_Client_Cmd_Line.py')]
        arg_list.append(' --action=')
        arg_list.append(action)
        arg_list.append(' --ip=')
        arg_list.append('10.10.8.89')
        arg_list.append(' --objectId=')
        arg_list.append(book)

        subprocess.call(["python", arg_list])

        print 'The program deleted successfully'

    else: 
        print 'The program is NOT deleted!'

我有一个图书ID列表。我想将这些数字传递给book变量以删除事件。

output of bookids samples : ['BOOK:688045640', 'BOOK:688045641', 'BOOK:688045642', 'BOOK:688045643', 'BOOK:688045644', 'BOOK:688045645', 'BOOK:688045646', 'BOOK:688045647']

我可以通过以下操作删除单个事件:

book = self.bids[0]

如何将bookids列表元素传递给book变量?

1 个答案:

答案 0 :(得分:0)

在您当前的代码中:

book = self.bids[0]
# ...
arg_list.append(book)

相当于

arg_list.append(self.bids[0])

self.bids列表中的单个项目附加到arg_list列表。要将整个self.bids列表添加到arg_list的末尾,请改用.extend方法:

arg_list.extend(self.bids)

另一种选择是使用+=赋值运算符,它将扩展现有列表:

arg_list += self.bids
顺便说一下,你的subprocess.call(["python", arg_list])有点奇怪。如the docs所示,它应该是

subprocess.call(["python"] + arg_list)

但是,导入UPNP_Client_Cmd_Line模块并直接调用其函数会更有效。