如下代码中有两处列表添加Interval对象的地方,其中一处用的append,一处用的+=其中用append的地方如果改为+=则报错,很奇怪啊,感觉两处应该都是一致的,有没有哪位大拿可以解释下呢?(当然此处不考虑性能,只考虑正确性)TypeError:'Interval'objectisnotiterable注:有问题的地方是倒数第六行到倒数第三行#这一行是ok的intervals.append(Interval(l,r))#下面这一行会报错TypeError:'Interval'objectisnotiterable#intervals+=(Interval(l,r))可以用+=的地方是out+=i,#-*-coding:utf-8-*-"""Givenacollectionofintervals,mergealloverlappingintervals.Forexample,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18]."""#Definitionforaninterval.classInterval(object):def__init__(self,s=0,e=0):self.start=sself.end=edefmerge(intervals):""":typeintervals:List[Interval]:rtype:List[Interval]"""out=[]foriinsorted(intervals,key=lambdai:i.start):#列表最后一个下标为-1ifoutandi.start
添加回答
举报
0/150
提交
取消