xxxmenger 发表于 2018-8-14 13:38:50

python3学习之列表

  列表:
  L.append(object)   追加
  备注:append将obj作为一个整体追加,无论obj是字符串、数字、字典、列表等,当是字典时全部插入,跟L.extend(iterable) 不同
  test =
  test1 =
  test.append(test1) : ]
  test.append(10) :
  L.copy()复制
  备注:没有参数,采用 test2 = test.copy()的方法获取新的列表,该列表在内存中被新建,有新id
  test2 = test.copy()

  In :>  Out: 140016313353992

  In :>  Out: 140016297719112
  L.extend(iterable)扩展(注意跟追加的区别)
  备注:iterable 可以是列表、元组、字典。它将iterable中的元素追加到L中,当是字典时只追加item,没有追加value
  In : test=
  In : test.extend()
  In : print(test)
  
  In : test.extend((7,8))
  In : print(test)
  
  In : test.extend({9:'aaa',10:'bbb'})
  In : print(test)
  
  L.insert(index, object)插入
  备注:在index前插入,index从0开始,obj作为一个整体,无论是列表、元组、字典等,当是字典时全部插入,跟L.extend(iterable) 不同
  In : print(test)
  
  In : test.insert(3,'aaaa')
  In : print(test)
  
  In : test.insert(3,)
  In : print(test)
  , 'aaaa', 4, 5, 6, 7, 8, 9, 10]
  L.pop()弹出
  L.remove(value) 移除
  L.clear()   清空
  备注:pop的参数是index,remove的参数是value,目的一样,clear是删除所有
  L.sort(key=None,reverse=False) 排序
  备注:默认是正序,即:1 2 3.... a b c...;reverse=True时是倒序,key参数一般不用,L中不可以包含嵌套列表、字典等
  L.count(value)计数
  备注:返回值等于value出现的次数
  L.index(value, ])查找
  备注:返回值是在指定的范围内第一次出现的等于value的index,stop=最大index+1才能判断到最后一个value
  In : print(tt)
  , 4]
  In : tt.index(4,2,7)
  Out: 6
  In : tt.index(4,2,6)
  ---------------------------------------------------------------------------
  ValueError                              Traceback (most recent call last)
  <ipython-input-118-900c1655e582> in <module>()
  ----> 1 tt.index(4,2,6)
  ValueError: 4 is not in list
  L.reverse()倒序
  备注:不是value排序,只是index倒个顺序,对列表的数据结构没要求,注意和sort的区别
  打印1:简单列表
  tt =
  In : for i in tt:
  .....:   print(i)
  .....:
  1
  2
  3
  打印2:value是成对出现时
  tt=[(2,3),(4,5),(6,7)]
  In : for a,b in tt:
  .....:   print(a,b)
  .....:
  2 3
  4 5
  6 7
页: [1]
查看完整版本: python3学习之列表