1.内置函数
官方文档-内置函数
1.1 查看list(列表)的内置方法
- dir(list)
# 查看list(列表)的内置方法
dir(list)
# 输出内置方法: dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__',
'__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__',
'__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index',
'insert', 'pop', 'remove', 'reverse', 'sort']
1.2 内置方法实例
- 内置方法的使用: 成员变量名.方法名(参数)
- count()-----------------------------------------查看
列表某一元素
的总数量
- index()-----------------------------------------查看
某一元素
的索引号
,只返回第一个的索引号 - 常用:reverse()-----------------------------列表元素:正序→倒序
- 常用:sort()--------------------------------根据大小排列
① sort()-------------------------从小到大
排列,默认sort(reverse=False)
② sort(reverse=True)-------从大到小
排列 - len()--------------------------------------------查看长度
- max()------------------------------------------返回最大值(数据类型必须统一)
- min()-------------------------------------------返回最小值(数据类型必须统一)
- sum()------------------------------------------返回总和(数字类型)
- sorted()---------------------------------------排序(数据类型必须统一)
- reversed()-----------------------------------倒序输出(转换完的是
对象
,得用list()输出
) - enumerate()--------------------------------以
(索引号,值)
的形式枚举出来,用list()
输出 - zip()------------------------------------------合并,(一维变二维,长度以短的为准)
# count()查看列表某一元素的总数量
# 定义name列表
name = ['张三','乔治','佩奇','王五','佩奇','孙六','佩奇','简单','佩奇']
# name列表里'乔治'元素的数量
name.count('佩奇')
# 输出 :4
# index():查询列表元素的索引
# 定义name列表
name = ['张三','乔治','佩奇','王五','佩奇','孙六','佩奇','简单','佩奇']
# (一个参数)index
name.index('佩奇')
# 输出 :2
# (三个参数)index:输出从3到7之间第一个'佩奇'元素的索引号
name.index('佩奇',3,7)
# 输出 :4
# reverse():正序→倒序
# 定义name列表
name = [1,2,3,4,5]
print(name)
# 输出 :[1, 2, 3, 4, 5]
name.reverse()
print(name)
# 输出 :[5, 4, 3, 2, 1]
# sort()根据大小排列
# 定义num列表元素
num = [2,7,9,3,56,12]
print(num)
# 输出 :[2, 7, 9, 3, 56, 12]
# sort():从小到大排列
num.sort()
print(num)
# 输出 :[2, 3, 7, 9, 12, 56]
# sort(reverse=True):从大到小排列
num.sort(reverse=True)
print(num)
# 输出 :[56, 12, 9, 7, 3, 2]
# len()查看长度
list1 = (1,2,3)
len(list1)
# 输出:3
# max()返回最大值
# 数字
list1 = [1,3,8,45,84,23]
max(list1)
# 输出:84
# str
list2 = ['q','a','d','f','z']
max(list2)
# 输出:'z'
# min()返回最小值
# 数字
min(list1)
1
# str
min(list2)
'a'
# sum()返回总和
# float类型
list2 = [1.2,3.4,5.5,6.5]
sum(list2)
# 输出:16.6
# Bool类型
list3 = [True,True,True]
sum(list3)
# 输出:3
# sorted()排序
list1 = [1, 3, 8, 45, 84, 23]
sorted(list1)
#输出:[1, 3, 8, 23, 45, 84]
# reversed() 倒序输出
list1 = [1, 3, 8, 45, 84, 23]
# 返回reversed(list1)的类型
type(reversed(list1))
# 输出:<class 'list_reverseiterator'>
# 利用list()函数输出
list(reversed(list1))
# 输出:[23, 84, 45, 8, 3, 1]
# enumerate() 以元组的形式枚举
list1 = [1, 3, 8, 45, 84, 23]
list(enumerate(list1))
# 输出:[(0, 1), (1, 3), (2, 8), (3, 45), (4, 84), (5, 23)]
# zip()合并-一维变二维
# 定义两个序列
list1 = [1,2,3,4,5,6]
list2 = [3,4,5,6,7,8,9,0]
# 合并序列 (以最短的为主,多余舍弃)
list(zip(list1,list2))
# 输出:[(1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8)]
2.元组转成列表
# tuple 转换成 list
list1 = (1,2,3)
type(list1)
# 输出:<class 'tuple'>
type(list(list1))
# 输出<class 'list'>