2017-10-15 189 views
-5

我将元素列表存储在列表中,我想创建一个if语句,以便我可以将一个字符串与列表进行比较以查看元素是否不匹配。检查元素是否是列表中的最后一个

下面是我用:

self.EPG_Channel = 'Channel 5' 
self.channel_str = ['BBC One, BBC Two, ITV, Channel 4, Channel 5'] 

我想创造的东西是这样的:

if not self.EPG_Channel == self.channel_str: 
    print "You are not in the last element in the list" 
else: 
    print "You are in the list element in the list so let do nothing..." 

我想知道你如何创建一个if语句来检查在列表中的元素用字符串来查看元素是否不在列表中的最后一个元素中?

回答

1

拿到最后一个元素我想你指的是这样的:

self.EPG_Channel = 'Channel 5' 
self.channel_str = ['BBC One, BBC Two, ITV, Channel 4, Channel 5'] 
if self.EPG_Channel == self.channel_str[-1]: 
    pass 
+0

这是我所期待的。谢谢!!!!!!! –

0

你可以用self.channel_str[-1] :)

相关问题