2013-02-13 194 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

相關問題