2017-10-15 188 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] :)

相關問題