2013-02-13 150 views
0

我想弄清楚如何将字符串与列表中的第0个元素(技术上是底部)进行比较。该名单也是一个类的实例,看起来像这样:将字符串与python中的类列表进行比较

#define class called GEL 
class GEL: 
    def __init__(self,eventtime,type): 
      self.eventtime=eventtime 
      self.type=type 

myList=[] 

当我添加的东西到列表:

myList.append(GEL(time,type)) ##Type can be either 'Arrival' or 'Departure' 

对于该列表将是(1.343432,“到达”) 所以我想比较'到达'和列表中的类型项目。

for i in range(5): ##loop for insertions and deletions 
    Type=[a.type for a in myList] ##Actually goes to the last type, but want first 
    if 'Arrival' in Type: 
      ##DO STUFF 
    else: 
      ##HELLO WORLD 
    #sortlist 
    myList.pop(0) 

刚刚在列表中获得第一个类型的正确方法是什么?

对不起,行话不好,我还在学习Python。

编辑:我想我可能已经解决了。它让我想要什么。如果有人能告诉我,如果这将是确定的:

if 'Arrival' in Type[0]: 
+0

其实,我想我可能刚刚解决了这个问题 – 2013-02-13 10:01:31

+0

您可以[回答自己的问题] (http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/),发表你的答案为(显然,)一个答案! – pradyunsg 2013-02-13 10:04:55

回答

2

我认为你需要这个

if mylist[0].type == 'Arrival': 
+0

谢谢。更容易。 – 2013-02-13 10:07:15

相关问题