在python中比较字符串和类列表

2024-06-11 10:17:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试找出如何将字符串与列表中的第0个元素(从技术上讲是底部)进行比较。列表也是一个类实例,如下所示:

#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,'Arrival') 所以我想把“Arrival”和列表中的类型项进行比较。你知道吗

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]:

Tags: 字符串inself元素类型列表forif