Python 列表函数
2018-01-18 15:03
267 查看
List列表函数
1. 共用函数(内建函数)
1. max 2. min 3. len 4. count(value) 5. index(value)
2. 列表专用函数
1. append() 2. insert(index,value) 3. extend() 4. pop(index) 5. remove(value) 6. clear() 7. copy() 8. reverse() 9. list()
公用函数
max()
功能:获取列表中的最大值
list1 = [12,23,45,76,8,99] print(max(list1))
99
list2 = [234,'355','43558378','isghg'] print(max(list2))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-b3c7275f80f1> in <module>() 1 list2 = [234,'355','43558378','isghg'] ----> 2 print(max(list2)) TypeError: '>' not supported between instances of 'str' and 'int'
注意:
Python 是强类型语言,可以识别数据类型,不同数据类型之间的操作有着严格的控制。所以字符串和数字的比较会报错。字符串的大小比较是根据python编码的数据集来比较的。
python3默认编码方式是utf8。
Unicode只是一套编码系统,包含所有字符集,却并不规定编码后的二进制代码如何存储。UTF-8是变长的编码方式,UTF-8是使用得最广泛的Unicode编码实现方式。
import sys print(sys.getdefaultencoding())
utf-8
list3 = ['a','b','c','%',')','~','由'] print(max(list3))
由
min()
功能:获取列表中最小的值
list1 = [12,234,6466,4566,-35] print(min(list1))
-35
list2 = ['sd','igsg','*'] print(min(list2))
*
list3 = [23,'2324',{'gd':"di"}] print(min(list3))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-70eb7e923730> in <module>() 1 list3 = [23,'2324',{'gd':"di"}] ----> 2 print(min(list3)) TypeError: '<' not supported between instances of 'str' and 'int'
len()
功能:获取列表的长度
list1 = [12,44,545,'24','46','34dftgt'] print(len(list1))
6
count()
功能:获取数值出现的次数
list2 = [1233,1233,'d','d',4,4,5] print(list2.count(1233))
2
index()
功能:获取元素的位置索引
list1 = [1,2,3,4,5,6,'a','b','c'] print(list1.index("a"))
6
列表专用函数
append()
功能:在列表最后添加一个元素 !!!直接改变原有列表。 返回值:无返回值
list1 = [1,2,3,4,5] list1.append(6) print(list1)
[1, 2, 3, 4, 5, 6]
insert(index,value)
功能:在列表指定位置添加元素 !!!直接改变原有列表 参数: index:要添加的位置 value:添加的元素值
list2 = ['a','a','a','a','a'] list2.insert(3,100) print(list2)
['a', 'a', 'a', 100, 'a', 'a']
extend()
功能:在列表后面添加列表 !!! 直接改变原有列表
list1 = [1,2,3,4,5] list2 = ['a','b','c','d','e'] list1.extend(list2) print(list1)
[1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e'] ['a', 'b', 'c', 'd', 'e']
pop()
功能:删除指定位置的元素 !!!直接改变原有列表
list1 = [1,2,3,4,5,6] list1.pop(0) print(list1)
[2, 3, 4, 5, 6]
remove()
功能:删除指定值的元素 !!!直接改变原有列表
list1 = [1,'1','1',1,'1','1'] list1.remove(1) print(list1)
['1', '1', 1, '1', '1']
clear()
功能:清空列表 !!!直接改变原有列表
list1 = ['23','556','575','hhh'] list1.clear() print(list1)
[]
copy()
功能:复制原有列表。真正意义上的复制,这个深拷贝。内存独立,不会互相干扰。
list1 = ['h','j','k','l'] print(list1,id(list1)) result = list1.copy() print(result,id(result)) result.clear() print(result) print(list1)
['h', 'j', 'k', 'l'] 1484096521480
['h', 'j', 'k', 'l'] 1484096520712[]['h', 'j', 'k', 'l']
list1 = [1,2,3] list2 = list1.copy() print(list2) list1[0] = 'aa' print(list1) print(list2) #list2并没有跟着list1的变化而变化,list自带的copy是深拷贝。
[1, 2, 3] ['aa', 2, 3] [1, 2, 3]
reverse()
功能:反转列表 !!!直接改变原有列表
list1 = [1,2,3,4,5] list1.reverse() print(list1) list2 = ['a','b','c','d','e'] print(list2[::-1])
[5, 4, 3, 2, 1] ['e', 'd', 'c', 'b', 'a']
list()
功能:将其他序列类型转化为列表
str1 = 'abcdefg' list1 = list(str1) print(list1)
['a', 'b', 'c', 'd', 'e', 'f', 'g']
set1= {'aa','bb','c','d','e'} print(list(set1))
['e', 'c', 'aa', 'bb', 'd']
touple1 = (1,2,'334dfd',4) print(list(touple1))
[1, 2, '334dfd', 4]
dict1 = {'a':'1','b':'2','c':3} print(list(dict1)) ##只改键
['a', 'b', 'c']
相关文章推荐
- python列表函数append(),extend()只改变对象的值,无返回值
- Python 列表(List)包含的函数与方法
- Dive Into Python 学习记录1-函数/模块导入/字典/列表/元组/字符串分割、连接、格式化/映射list/
- 1.python语法基础-列表、数组、函数、元组和控制语句
- Python 使用reduce()函数实现,列表内的数相乘
- Python在函数中使用*和**接收元组和列表
- Python入门 第三天(set、函数、切片、迭代、列表生成式)
- Python:列表函数
- python两种列表元素去重函数性能比较
- Python列表及相关函数
- 【python初识】列表与函数模块
- 【Python】向函数传递列表
- python练习笔记——用函数对列表奇偶分类,且过程不增加新列表
- python 禁止函数修改列表的实现方法
- python 列表函数
- python 列表函数
- Python——可变类型与不可变类型(即为什么函数默认参数要用元组而非列表)
- python的sorted 函数、python列表排序
- python元组函数、列表
- Python3 利用函数 实现阶乘、列表中删除指定元素、等差数列求和