Python 数据类型之list

1.1数值型

int(x) 返回一个整数
float(x) 返回一个浮点数
complex(x)、complex(x,y) 返回一个复数
bool(x) 返回布尔值,前面讲过False等价的对象

1.2对象函数的处理

round(),总结:四舍六入,5取偶(最近的偶数)

print(round(2.5)) #2
print(round(2.5001)) #3
print(round(2.6)) #3
print(round(-2.5)) #-2
print(round(-2.5001)) #-3
print(round(-2.6)) #-3

int(),总结只取整数部分

print(int(3.5)) #3
print(int(3.4)) #3
print(int(3.6)) #3

//整除,总结整除并且向下取整

print(7//2, 7//-2, -7//2, -(7//2)) #3 -4 -4 -3
print(2//3, -2//3, -1//3) #0 -1 -1  

math模块总结 floor()向下取整 ,ceil()向上取整

print(math.floor(2.5), math.floor(-2.5)) #2 -3
print(math.ceil(2.5), math.ceil(-2.5))#3 -2

1.3数字处理函数

max(...)
    max(iterable, *[, default=obj, key=func]) -> value
    max(arg1, arg2, *args, *[, key=func]) -> value
min(...)
    min(iterable, *[, default=obj, key=func]) -> value
    min(arg1, arg2, *args, *[, key=func]) -> value
pow(x, y, z=None, /)
    Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
    文档上说使用三个参数时更高效
math.sqrt()
sqrt(...)
    sqrt(x)   
    Return the square root of x.

1.4类型判断

type:返回类型,而不是字符串 

type(object_or_name, bases, dict)
type(object) -> the object's type
type(name, bases, dict) -> a new type
type隐式转换
type(1+True)  #返回int

  

isinstance:相比于type可以判断子类

isinstance(obj, class_or_tuple, /)
    Return whether an object is an instance of a class or of a subclass thereof.
    
    A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
    check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
    or ...`` etc.
i.e:
isinstance(6, (str, bool, int))

1.5列表

1.5.1列表特性

一个队列,一个排列整齐的队伍 
列表内的个体称作元素,由若干元素组成列表 
元素可以是任意对象(数字、字符串、对象、列表等) 
列表内元素有顺序,可以使用索引 
线性的数据结构 
使用 [ ] 表示
列表是可变的

1.5.2列表定义

list() -> new empty list 
list(iterable) -> new list initialized from iterable's items #迭代器也是可迭代对象,所以也能放迭代器
lst = list()
lst = [] 
lst = [2, 6, 9, 'ab'] 
lst = list(range(5))

1.5.3列表索引访问

lst = [1,3,5,7,9,'a','b','c','d']

索引,也叫下标
正索引:从左至右,从0开始,为列表中每一个元素编号 
负索引:从右至左,从-1开始 
正负索引不可以超界,否则引发异常IndexError 
为了理解方便,可以认为列表是从左至右排列的,左边是头部,右边是尾部,左边是下界,右边是 上界

访问方式:
list[index] ,index就是索引,使用中括号访问
lst[-1] #'d'
lst[1] #3
lst[0:4] #[1, 3, 5, 7]
lst[-4:] #['a', 'b', 'c', 'd']

1.5.4列表查询

lst = [1,3,5,7,9,'a','b','c','d']

index(value,[start,[stop]]) --->return first index of value.
1.通过值value,从指定区间查找列表内的元素是否匹配
2.匹配第一个就立即返回索引
3.匹配不到,抛出异常ValueError 

lst.index('a',1,6)  #output 5
lst.index('a',-9,-3) #output 5
总结index按照从左到右的顺序查找,所以负数的时候也要按照次顺序查找
count(value) -> integer -- return number of occurrences of value
返回列表中匹配value的次数 

时间复杂度 
1.index和count方法都是O(n) 
2.随着列表数据规模的增大,而效率下降 


如何返回列表元素的个数?如何遍历?如何设计高效? 
len()  --->Return the number of items in a container;类似于计数器

1.5.5列表元素修改

索引访问修改
list[index] = value  #通过下表对值重新赋值
lst[5]="aaaa"          #索引不要超界

1.5.6列表增加插入元素

append(object) -> None -- append object to end
1.列表尾部追加元素,返回None 
2.返回None就意味着没有新的列表产生,就地修改 
3.时间复杂度是O(1) 
lst.append('e')


insert(index, object) -- insert object before index
1.在指定的索引index处插入元素object 
2.返回None就意味着没有新的列表产生,就地修改
3.时间复杂度是O(n) p 
4.索引能超上下界吗? 1.超越上界,尾部追加 2.超越下界,头部追加

1.5.6列表增加插入元素

extend(...) method of builtins.list instance
    L.extend(iterable) -> None -- extend list by appending elements from the iterable
1.将可迭代对象的元素追加进来,返回None 
2. 就地修改 
ie:
lst.extend(range(10,20,2))


+ -> list
1.连接操作,将两个列表连接起来 
2.产生新的列表,原列表不变 
3.本质上调用的是__add__()方法 
ie:
lst5=[1,2,3,4]
lst6=['a',['b','c']]
lst7=lst4+lst5     #output[1, 2, 3, 4, 'a', 'b']

lst7[0]=100
lst7               #output[100, 2, 3, 4, 1, 2, 3, 4]
lst4               #output[1, 2, 3, 4]

 * -> list 
1.重复操作,将本列表元素重复n次,返回新的列表
ie:
lst2 = ['a','c',[1,2]]*2 #output['a', 'c', [1, 2], 'a', 'c', [1, 2]]
lst2[0]='aaaaa'          #output['aaaaa', 'c', [1, 2], 'a', 'c', [1, 2]]
lst2[2][0]=11111       #output['aaaaa', 'c', [11111, 2], 'a', 'c', [11111, 2]]

总结简单的object,list重复n次的时候,重复项已经独立出来了,更改一个 ,不会全部变
        复杂的object,list中嵌套list,重复项未独立出来,更改嵌套的list中一个值,全部变化

1.5.7列表删除元素

remove(...) method of builtins.list instance
    L.remove(value) -> None -- remove first occurrence of value.
    Raises ValueError if the value is not present
1.从左至右查找第一个匹配value的值,移除该元素,返回None 
2. 就地修改 
3.效率是O(n)

pop(...) method of builtins.list instance
    L.pop([index]) -> item -- remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.
1.不指定索引index,就从列表尾部弹出一个元素
2.指定索引index,就从索引处弹出一个元素,索引超界抛出IndexError错误 
3.效率,制定索引O(n),不指定索引O(1)

clear(...) method of builtins.list instance
    L.clear() -> None -- remove all items from L
1.清除列表所有元素,剩下一个空列表
2.频繁clear会造成内存gc,当list太大是会影响性能

1.5.8列表的其他操作

reverse(...) method of builtins.list instance
    L.reverse() -- reverse *IN PLACE*
1. 将列表元素反转,返回None 
2.就地修改 


class reversed(object)
    reversed(sequence) -> reverse iterator over values of the sequence
    Return a reverse iterator
ie:
tmp = reversed(lst)
tmp    #output <list_reverseiterator at 0x7fdef40fc4e0>
for i in tmp:
    print(i)

总结
在查询帮助文档的时候会显示function,method 再此以reverse()和reversed()做(i,e)
reverse是method;与类和实例有绑定关系的function都属于方法(method)定义的lst是一个类的实例,所以可以调用lst.reverse()
reversed()是function,与类和实例无绑定关系的function都属于函数(function),函数一般需要传递一个参数,所以可以调用reversed(lst)

reversed 返回一个iterator,all of iterator is iterable,list函数传递一个iterable,所以可以传递迭代器iterator作为参数,可以打印reversed的值了

1.5.9列表排序操作

sort(...) method of builtins.list instance
    L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
    true 1 false 0
1.对列表元素进行排序,就地修改,默认升序 
2.reverse为True,反转,降序 
3.key一个函数,指定key如何排序 
    lst.sort(key=functionname)

1.5.10 in

in 成员运算符
1 in [1,2,3,4,5,[2,3]]
返回True

[2,3] in [1,2,3,4,5,[2,3]]
返回True

 1.5.11深浅copy

#浅copy的引用类型,引用的内存地址相同
lst3 = [1,[2,3],4,5]
lst4 = lst3.copy()
id(lst3[1])==id(lst4[1])

  

本文为原创文章,转载请标明出处
原文地址:https://www.cnblogs.com/harden13/p/8633051.html