aidd

导航

 

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

1.用途:多个装备、多个爱好、多门课程

2.定义:[]内可以有多个任意类型的值,逗号分隔元素

python

# my_girl_friend = list(['jason','tank','sean'])
my_girl_friend = ['jason', 'tank', 'sean']

print(f"my_girl_friend: {my_girl_friend}")

default

my_girl_friend: ['jason', 'tank', 'sean']

python

l = list('hello nick')
print(f"l: {l}")

default

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

3.常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)、其他操作(了解)三个部分。

1.1 优先掌握(*****)

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

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

python

# list之索引取值
name_list = ['nick', 'jason', 'tank', 'sean']
name_list[0] = 'nick handsom'
# name_list[1000] = 'tank sb'  # 报错

print(f"name_list[0]: {name_list[0]}")

default

name_list[0]: nick handsom

2.切片

python

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

print(f"name_list[0:3:2]: {name_list[0:3:2]}")

default

name_list[0:3:2]: ['nick', 'tank']

3.长度

python

# list之长度
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"len(name_list): {len(name_list)}")

default

len(name_list): 4

4.成员运算in和not in

python

# list之成员运算in和not in
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"'tank sb' in name_list: {'tank sb' in name_list}")
print(f"'nick handsome' not in name_list: {'nick handsome' not in name_list}")

default

'tank sb' in name_list: False
'nick handsome' not in name_list: True

5.追加值

python

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

print(f"name_list: {name_list}")

default

name_list: ['nick', 'jason', 'tank', 'sean', 'tank sb']

6.删除

python

# list之删除
name_list = ['nick', 'jason', 'tank', 'sean']
del name_list[2]

print(f"name_list: {name_list}")

default

name_list: ['nick', 'jason', 'sean']

7.循环

python

# list之循环
name_list = ['nick', 'jason', 'tank', 'sean']

for name in name_list:
    print(name)

default

nick
jason
tank
sean

1.2 需要掌握(****)

  1. insert
  2. pop
  3. remove
  4. count
  5. index
  6. clear
  7. copy
  8. extend
  9. reverse
  10. sort

1.insert()

python

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

print(f"name_list: {name_list}")

default

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

2.pop()

python

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

print(f"name_list.pop(1): {name_list.pop(1)}")
print(f"name_list: {name_list}")

default

name_list.pop(1): jason
name_list: ['nick', 'tank', 'sean']

3.remove()

python

# list之remove()
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.remove('nick'): {name_list.remove('nick')}")
print(f"name_list: {name_list}")

default

name_list.remove('nick'): None
name_list: ['jason', 'tank', 'sean']

4.count()

python

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

print(f"name_list.count('nick'): {name_list.count('nick')}")

default

name_list.count('nick'): 1

5.index()

python

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

print(f"name_list.index('nick'): {name_list.index('nick')}")

default

name_list.index('nick'): 0

6.clear()

python

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

print(f"name_list: {name_list}")

default

name_list: []

7.copy()

python

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

print(f"name_list.copy(): {name_list.copy()}")

default

name_list.copy(): ['nick', 'jason', 'tank', 'sean']

8.extend()

python

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

print(f"name_list: {name_list}")

default

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

9.reverse()

python

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

print(f"name_list: {name_list}")

default

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

10.sort()

python

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

print(f"name_list: {name_list}")

name_list.sort(reverse=True)
print(f"name_list_reverse: {name_list}")

default

name_list: ['jason', 'nick', 'sean', 'tank']
name_list_reverse: ['tank', 'sean', 'nick', 'jason']

4.存一个值or多个值:多个值

5.有序or无序:有序

python

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

default

first:4522187016
second:4522187016

6.可变or不可变:可变数据类型

posted on 2023-05-30 17:03  开源共享  阅读(10)  评论(0编辑  收藏  举报