为了账号安全,请及时绑定邮箱和手机立即绑定

python中list 的 += 和 append 等价么?

python中list 的 += 和 append 等价么?

呼如林 2019-05-25 17:05:48
如下代码中有两处列表添加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
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 477 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号