Python 检查该元素是否是列表中的最后一个元素

Python 检查该元素是否是列表中的最后一个元素,python,python-2.7,Python,Python 2.7,我将元素列表存储在列表中,我想创建一个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 e

我将元素列表存储在列表中,我想创建一个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语句,用一个字符串检查列表中的元素,看看该元素是否不在列表中的最后一个元素中?

你可以用self.channel\u str[-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

小心,若列表为空,将引发索引器。