ℙℕℤℝ 2008-10-31 05:55
浏览 1039
已采纳

Python 中追加与扩展列表方法之间的区别

What's the difference between the list methods append() and extend()?

转载于:https://stackoverflow.com/questions/252703/difference-between-append-vs-extend-list-methods-in-python

  • 写回答

24条回答 默认 最新

  • 关注

    append: Appends object at the end.

    x = [1, 2, 3]
    x.append([4, 5])
    print (x)
    

    gives you: [1, 2, 3, [4, 5]]


    extend: Extends list by appending elements from the iterable.

    x = [1, 2, 3]
    x.extend([4, 5])
    print (x)
    

    gives you: [1, 2, 3, 4, 5]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(23条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制