027 列表类型内置方法

列表类型内置方法(list)

1.作用

用来描述多个装备、多个爱好、多种状态等

2.定义

在[]里可以存放多个任意类型的值,逗号分隔元素

hobby_list = ['run','play','read']
hobby_list = list ['run','play','read']

print(f'my hobby_list:{hobby_list}')
# my hobby_list:['run','play','read']

3.常用操作+内置方法

**优先掌握**

1.按索引取值

2.切片(split)

3.长度(len)

4.成员运算(is/is not)

5.for循环

6.删除(del)

7.追加(append)

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

# list之索引取值
hobby_list = ['play', 'swimming', 'dancing', '666']

print(1, hobby_list[-1])
hobby_list[-1] = '233'  # 索引修改
print(2, hobby_list[-1])

2.切片(split)

# list之切片
print(hobby_list[:])  # ['play', 'swimming', 'dancing', '666']
print(hobby_list[::-1])  # 从右到左  # ['666', 'dancing', 'swimming', 'play']

# ['play', 'swimming', 'dancing', '233']
# ['233', 'dancing', 'swimming', 'play']

3.长度(len)

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

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

# len(name_list): 4

4.成员运算(in/not in)

# 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}")

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

5.循环(for)

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

for name in name_list:
    print(name)
    
##结果    
nick
jason
tank
sean

6.删除(del)

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

print(f"name_list: {name_list}")

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

7.追加值(append)

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

print(f"name_list: {name_list}")

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

**需要掌握**

1.insert(插入,在列表的某一个位置插入一个元素)

2.pop(删除元素.默认删除列表最后一个元素)

3.remove(移除元素)

4.count(计数.u哦你过来计数列表里面某一元素出现的次数)

5.index(索引.返回列表里某一元素的位置)

6.claar(清空列表)

7.copy(复制列表)

8.extend(继承,也就是两个列表何在一起)

9.reverse(反转列表的元素,从右到左进行排序)

10.sort(排序.默认升序排)

1.insert(插入元素)

hobby_list = ['run','play','read']
hobby_list.insert(1,'sleep')

print(hobby_list)
# ['run', 'sleep', 'play', 'read']

2.pop(删除元素)

# pop默认删除的是最后一个元素,但是也可以指定索引的位置
hobby_list = ['run','play','read']
hobby_list.pop()
print(hobby_list)
# ['run', 'play']

hobby_list = ['run','play','read']
hoppy_list.pop(0)
print(hobby_list)
# ['play','read']

3.remove(移除)

hobby_list = ['run','play','read']
hobby_list.remove('run')
print(hobby_list)
# ['play','read']

4.count(统计某一元素在列表出现的次数)

hobby_list = ['run','play','read']

print(hobby_list.count('run'))

# 1

5.index()

hobby_list = ['run','play','read']
print(hobby_list(play))

# 1

6.clear()

hobby_list = ['run','play','read']
hobby_list.clear()

print(hobby_list)

# []

7.copy()

hobby_list = ['run','play','read']
hobby_list2 = hobby_list.copy

print(hobbby_list2)

# ['run','play','read']

8.extend()

hobby_list = ['run','play','read']
hobby_list2 = ['sing']

hobby_list.extend(hobby_list2)

print(hobby_list)

# ['run', 'play', 'read', 'sing']

9.reverse()

hobby_list = ['run','play','read']

hobby_list.reverse()

print(hobby_list)

# ['read', 'play', 'run']

10.sort()

hobby_list = ['run','play','read']

hobby_list.sort()
print(hobby_list)
['play', 'read', 'run']

4.列表是存多个值的

5.列表是有序的

hobby_list = ['run','play','read']
print(id(hobby_list))
hobby_list[2] = 'sing'
print(id(hobby_list))

#结果
2855383215368
2855383215368

6.可变数据类型

上一篇:华为运营商级路由器配置示例 | EVdPdNd VPLS over SRv6 BE(CE双归接入)


下一篇:027:简单的SumArray