我试图弄清楚如何将字符串与列表中的第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'和列表中的类型项。
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 :(得分:2)
我认为你只需要这个
if mylist[0].type == 'Arrival':