列表类型内置方法

一、列表类型内置方法(list)

  1. 用途:多个装备、多个爱好、多门课程、甚至多个女朋友
  2. 定义:[]内可以有多个任意类型的值.逗号分隔元素
# my_girl_friend = list([]'jason','tank','sean'])
my_girl_friend = ['jason','tank','sean']

print(my_girl_friend)

['jason', 'tank', 'sean']

l = list('hello nick')
print(l)

['h', 'e', 'l', 'l', 'o', ' ', 'n', 'i', 'c', 'k']

  1. 常用操作+内置方法:常用操作和内置方法分为优先掌握、需要掌握、其他操作三分部分。

一、优先掌握

 1. 按索引取值(正向取值+反向取值),即可存也可以取
# list 之索引取值
name_list = ['nick','jason','tank','sean']
name_list[0] = 'nick handsome'
# name_list(1000) = 'tank  sb'#报错
print(name_list[0])

nick handsome

  1. 切片
# list之切片
name_list = ['nick','jason','tank','sean']

print(name_list[0:3:2])

['nick', 'tank']

  1. 长度len
# list之长度
name_list = ['nick','jason','tank','sean']
print(len(name_list))

4

  1. 成员运算in 和not in
#list之成员运算in 和not in
name_list = ['nick','jason','tank','sean']
print('nick handsome' in name_list)
print('tank sb' not in name_list)

False
True

  1. 追加值(append)
#list之追加值(append)
name_list = ['nick','jason','tank','sean']
name_list.append('nick handsome')
print(name_list)

['nick', 'jason', 'tank', 'sean', 'nick handsome']

  1. 删除
#list之删除
name_list = ['nick','jason','tank','sean']
del name_list[0]
print(name_list)

['jason', 'tank', 'sean']

  1. 循环
#list之循环
name_list = ['nick','jason','tank','sean']
for i in name_list:
  print(i)

nick jason tank sean

二、需要掌握

  1. insert(添加)
  2. pop
  3. remove
  4. count
  5. index
  6. clear
  7. copy
  8. extend
  9. reverse
  10. sort
  11. insert()
# list之insert()
name_list = ['nick','jason','tank','sean']
name_list.insert(1,'handsome')
print(name_list)

['nick', 'handsome', 'jason', 'tank', 'sean']

  1. pop(默认删除最后一个)
# list之pop(),pop()默认删除最后一个元素
name_list = ['nick','jason','tank','sean']
print(name_list.pop(0))
print(name_list)

nick ['jason', 'tank', 'sean']

  1. remove(移除)
# list之remove()
name_list = ['nick', 'jason', 'tank', 'sean']
#print(name_list.remove('nick'))(当直接些这句代码时 返回none)
name_list.remove('nick')
print(name_list)

['jason', 'tank', 'sean']

  1. count()
# list之count()
name_list = ['nick','jason','tank','sean']
print(name_list.count('nick'))

1

  1. index()
# list之index()
name_list = ['nick','jason','tank','sean']
print(name_list.index('nick'))

0

  1. clear()
# list之clear()
name_list = ['nick','jason','tank','sean']
name_list.clear
print(name_list)

[]

  1. copy()
# list之copy()
name_list = ['nick','jason','tank','sean']
print(name_list.copy())

['nick', 'jason', 'tank', 'sean']

  1. extend(扩展)
# list之extend()
name_list = ['nick','jason','tank','sean']
name_list2 = ['nick handsome']
name_list.extend(name_list2)
print(name_list)

['nick', 'jason', 'tank', 'sean', 'nick handsome']

  1. reverse()
# list之reverse()
name_list = ['nick','jason','tank','sean']
name_list.reverse()
print(name_list)

['sean', 'tank', 'jason', 'nick']

  1. sort()
# list之sort(),使用sort列表的元素必须是同类型的
name_list = ['nick','jason'.'tank','sean']
name_list.sort()
print(name_list)
name_list0sort(reverse=True)
print(name_list)

['jason', 'nick', 'sean', 'tank'] ['tank', 'sean', 'nick', 'jason']

存一个值or多个值:多个在

有序or无序:有序

可变不可变:可变数据类型

hobby_list = ['read', 'run', 'girl']
print(f'first:{id(hobby_list)}')
hobby_list[2] = ''
print(f'second:{id(hobby_list)}')

first:4522187016 second:4522187016

posted @ 2019-08-05 22:11  Daoqi  阅读(118)  评论(0编辑  收藏  举报