sihangjun

python中的append的用法

append在python中一个很重要的用法,会大量使用,但是其中有些细节需要注意。首先说说一些最简单的用法:
append的实例用法:

append()用法示例:

>>> mylist = [1,2,0,\'abc\']

>>> mylist

[1, 2, 0, \'abc\']

>>> mylist.append(4)

>>> mylist

[1, 2, 0, \'abc\', 4]

>>> mylist.append(\'haha\')

>>> mylist

[1, 2, 0, \'abc\', 4, \'haha\']
 
还需要注意的是使用完append()函数以后的新的列表
weibo=[]
wei=[1,23,34,5,6,6,6,624,624,32,534,352,2352,2525,2152]
weibo.append(wei)
print weibo
返回结果:[[1, 23, 34, 5, 6, 6, 6, 624, 624, 32, 534, 352, 2352, 2525, 2152]]
print type(weibo)
返回结果:<type \'list\'>
若此时要判断wei列表与weibo列表是否相同我们如果使用isinstance函数就会出现错误
print isinstance(weibo,wei)
返回结果:TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
因为isinstance()比较必须是一个类,类型,或元组的类和类型
 
在python还有一个相似的extend()其只能对列表进行黏贴。
 
 

分类:

技术点:

相关文章:

  • 2021-11-29
  • 2021-11-29
  • 2021-10-13
  • 2022-12-23
  • 2021-11-29
  • 2021-09-11
  • 2022-12-23
  • 2021-11-29
猜你喜欢
  • 2021-11-29
  • 2022-12-23
  • 2021-11-29
  • 2021-11-29
  • 2021-11-29
  • 2021-09-03
  • 2022-02-03
相关资源
相似解决方案