列表类型内置方法

列表类型内置方法(list)

一、作用:

用来存放多个值,如多个爱好,多门课程等

二、定义方式

# 第一种方式
hobby_list = ['play', 'swimming', 'dancing'] # 实际存储的是内存地址
# 第二种方式,将字符打散
lt = list('randysun') # 参数放入可迭代对象

结果:
['play', 'swimming', 'dancing']
['r', 'a', 'n', 'd', 'y', 's', 'u', 'n']

三、常用内置方法

主要内置方法

  1. 索引取值(正向取值+反向取值),即可存也可以取

    hobby_list = ['play', 'swimming', 'dancing']
    print(hobby_list[-1])
    hobby_list[0] = '2233'
    print(hobby_list)
    
    

    结果:

    dancing
    ['2233', 'swimming', 'dancing']

  2. 切片

hobby_list = ['play', 'swimming', 'dancing']
print(hobby_list[::2])
print(hobby_list[::-1]) # 逆序

结果:

['play', 'dancing']
['dancing', 'swimming', 'play']

  1. 长度len()
hobby_list = ['play', 'swimming', 'dancing']
print(len(hobby_list))

结果

3

  1. for循环
hobby_list = ['play', 'swimming', 'dancing']
for hooby in hobby_list:
    print(hooby)

结果:

play
swimming
dancing

  1. in / not in(判断值是否存在)
hobby_list = ['play', 'swimming', 'dancing']
print('swimming' in hobby_list)

结果:
True

  1. 删除del

    del hobby_list[-1] # 删除指定值,或删除所有
    print(hobby_list)
    

    结果:

    ['play', 'swimming']

  2. 追加append()

    hobby_list.append('123')
    print(hobby_list)
    

    结果:

    ['play', 'swimming', 'dancing', '123']

需要掌握方法

  1. 统计列表中某一元素出现的次数count(),没有为0

    hobby_list = ['play', 'swimming', 'dancing', '1', '1']
    print(hobby_list.count(''))
    print(hobby_list.count('1'))
    

    结果:

    0
    2

  2. 扩展列表extends(),+相当于extends

hobby_list = ['play', 'swimming', 'dancing', '1', '1']
hobby_list.extend([1,2,3,4])
print(hobby_list)

结果:

['play', 'swimming', 'dancing', '1', '1', 1, 2, 3, 4]

  1. 清除clear()
hobby_list = ['play', 'swimming', 'dancing', '1', '1']
print(hobby_list.clear())

结果:
None

  1. 复制列表copy(),id一样
hobby_list = ['play', 'swimming', 'dancing', '1', '1']
hobby_list2 = hobby_list.copy()
print(hobby_list2)

结果:

['play', 'swimming', 'dancing', '1', '1']

  1. 删除pop(),默认删除最后一个

    hobby_list = ['play', 'swimming', 'dancing', '1', '1']
    del_res = hobby_list.pop() # 指定索引删除第几个,将删除的值返回
    print(del_res)
    print(hobby_list)
    

    结果:
    ['play', 'swimming', 'dancing', '1']

  2. 索引 index()

hobby_list = ['play', 'swimming', 'dancing', '1', '1']

print(hobby_list.index('play')) # 值不存在则会报错,可以与异常一起连用

结果:

0

  1. 插入 insert()
hobby_list = ['play', 'swimming', 'dancing', '1', '1']

hobby_list.insert(0, "1") # 指定位置插入值
print(hobby_list)

结果:

['1', 'play', 'swimming', 'dancing', '1', '1']

  1. 移除remove()根据值删除
hobby_list = ['play', 'swimming', 'dancing', '1', '1']
hobby_list.remove('1') # 只会删除一个

结果:

['play', 'swimming', 'dancing', '1']

  1. 翻转reverse()

    hobby_list = ['play', 'swimming', 'dancing']
    hobby_list.reverse() # 使用切片也可以反转
    print(hobby_list)
    

    结果:
    ['dancing', 'swimming', 'play']

  2. 排序默认升序sort()

``` python
num_list = [4, 2, 7, 4, 44, 432, 1, 35]
num_list.sort()
print(num_list)

```

结果:
[1, 2, 4, 4, 7, 35, 44, 432]

四、存在一个值还是一个值

多个值

五、有序 or 无序

有序

六、可变 or 不可变

可变

lis = [1, 1, 2, 3, 4, 5]
print(lis)
print(id(lis))
lis.append(100)
print(lis)
print(id(lis))

结果:

[1, 1, 2, 3, 4, 5]
2317614604936
[1, 1, 2, 3, 4, 5, 100]
2317614604936

七、总结

7.1 常用操作

  1. 按索引取值(正向取值+反向取值),即可存也可以取
  2. 切片
  3. 长度len
  4. 成员运算in和not in
  5. 追加append
  6. 删除del
  7. 循环

7.2 常用方法

  1. insert
  2. pop
  3. remove
  4. count
  5. index
  6. clear
  7. copy
  8. extend
  9. reverse
  10. sort
posted @ 2019-08-05 18:35  RandySun  阅读(220)  评论(0编辑  收藏  举报