您的位置:首页 > 编程语言 > Python开发

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 list