python基本数据类型之一:列表list
使用英文逗号,分隔不同元素,使用方括号[]括起来
元素支持不同的数据类型,若均是list,则为嵌套列表
空列表:[]
基本数据类型
数字:int、float、bool、complex(复数)
string 字符串
bool 布尔型
List 列表
Tuple 元组
set集合
Dict 字典
不可变数据:Number、String、Tuple
可变数据:List、Dictionary、Setlists=[1,1.2,True,complex(1,4),"26",[3,8], (4,),{5},{"dict":6}] for i in lists: print(type(i)) # 不会认为子类是父类的一种 # isinstance # 子类是父类的一种 ==v时返回True # <class 'int'> # <class 'float'> # <class 'bool'> # <class 'complex'> # <class 'str'> # <class 'list'> # <class 'tuple'> # <class 'set'> # <class 'dict'>
1、list运算
与字符串相同
-
截取:
list[头下标:尾下标]或者list[下标]或list[头下标:尾下标:步长]
索引值从0开始,len(list)-1结束;-1为从末尾开始位置
截取是,包括头下标对应元素,不包括尾下标对应的元素
步长默认是1
列表和字符串索引
-
+连接
-
*重复操作 -
in判断x in list 返回bool类型
for x in list迭代
tiny_list=[123,2.1,False,"abc",[],(),{}, {2}]
print(tiny_list) # 整个列表
print(tiny_list[1]) # 索引=1的元素2.1
print(tiny_list[2:]) # 索引=2及之后所有元素
print(tiny_list[:1]) # 索引=1(不包括)之前的所有元素 [123]
# print(tiny_list[10]) # IndexError: list index out of range
print(tiny_list[::2]) # 步长为2,[123, False, [], {}]
print(tiny_list[::-1]) # 步长-1,实现列表倒序
print(tiny_list[-1:]) # 从末尾开始到末尾,[{2}]
print(tiny_list[-1::-1]) # 从末尾开始,逆向到头,即列表倒序
print(tiny_list[0:2]*2) # 重复2次,[123, 2.1, 123, 2.1]
print(tiny_list[:1]+tiny_list[1:]) # +,拼接列表
2、更新删除列表
# 方式1:=赋值
tiny_list[1]="update1"
print(tiny_list) # [123, 'update1', False, 'abc', [], (), {}, {2}]
# 方式2:list.append(object) 列表末尾添加object
tiny_list.append(78)
print(tiny_list) # [123, 'update1', False, 'abc', [], (), {}, {2}, 78]
# 方式3:list.extend(iterable) iterable的每一元素逐一添加list末尾
tiny_list.extend("extend")
print(tiny_list) # [123, 'update1', False, 'abc', [], (), {}, {2}, 78, 'e', 'x', 't', 'e', 'n', 'd']
# 方式4:list.insert(index,object) 在索引index处添加object,其后元素依次后移
# 若index>=len(list),则末尾添加;若index或<-len(list),则首位添加object
tiny_list.insert(-30,"insert")
print(tiny_list)
# 删除元素
# 方式1:del
del tiny_list[5:]
print(tiny_list) # ['insert', 123, 'update1', False, 'abc']
# 方式2:list.remove(value) 若value不存在list,则报错
tiny_list.remove("insert")
print(tiny_list) # [123, 'update1', False, 'abc']
# tiny_list.remove("insert") # ValueError: list.remove(x): x not in list
# 方式3:list.pop(index=-1) index默认-1,即移除最后一个元素;若index超出,则报错
tiny_list.pop(-1)
print(tiny_list) # [123, 'update1', False]
# tiny_list.pop(3) # IndexError: pop index out of range
# 方式4:list.clear() 清除list中所有元素
tiny_list.clear()
print(tiny_list) # []
3、列表比较
引入operator模块的eq方法
# 列表比较
import operator
# 等同于operator.eq(a,b) a==b
print(operator.eq(tiny_list,tiny_list2)) # True
print(tiny_list>tiny_list2)
对象的比较运算、逻辑运算、数学运算以及序列运算,在operator模块中都有高效率函数与之对应
Py3笔记3:数据类型&运算符
Python3 operator 模块 | 菜鸟教程
4、其他函数
4.1 Python内置函数
- 内置
len(list)获取list长度 - 内置
max(list)、min(list)获取最大\最小元素(前提元素可比较大小,否则报错TypeError: '>' not supported between instances of 'str' and 'int') - 内置
list(seq)将序列seq转为列表list("abc")=['a', 'b', 'c']
4.2 list其他方法
tiny_list=['insert', 123, 'update1', False, 'abc', 'e', 'x', 't', 'e', 'n', 'd']
# list.count(object) 返回object在list中出现次数,没有返回0
print(tiny_list.count("e")) # 2
# list.index(value,start,end) 从列表中找出第一个value对应的索引
# 没有则报错ValueError: 'value' is not in list
print(tiny_list.index("e")) # 5
# list.reverse() 原列表反序
tiny_list.reverse()
print(tiny_list) # ['d', 'n', 'e', 't', 'x', 'e', 'abc', False, 'update1', 123, 'insert']
# list.sort(key=None,reverse=False) 原列表排序
# key 用来进行比较的元素,只有一个参数,具体函数参数可以取自可迭代对象中
# reverse 排序规则,默认False升序,True降序
tiny_list.remove(123)
tiny_list.remove(False)
tiny_list.sort() # 可比较大小,否则报错TypeError: '<' not supported between instances of 'bool' and 'str'
print(tiny_list) # ['abc', 'd', 'e', 'e', 'insert', 'n', 't', 'update1', 'x']
users_list=[
{"name": "John", "age": 23},
{"name": "Jack", "age": 18},
{"name": "Bob", "age": 29},
]
# users_list.sort() # TypeError: '<' not supported between instances of 'dict' and 'dict'
# 提取元素中的age
def getName(ele):
return ele["age"]
users_list.sort(key=getName)
print(users_list)
# [
# {'name': 'Jack', 'age': 18},
# {'name': 'John', 'age': 23},
# {'name': 'Bob', 'age': 29}
# ]
纯字符串排序,按ASCII码排序:先按照首字母排序,若首字母相同,再按照第二字母排序,依次类推;
数值<大写字母<小写字母
引申:
chr(number)返回ascii=number的字符(string类型)
ord(char)返回char字符对应的ascii值(int类型)
# list.copy() # 深拷贝VS浅拷贝
tiny_list2=tiny_list.copy()
print(tiny_list2)
# id(object) 获取对象的唯一标识,一般代表内存中存储地址
print(id(tiny_list),id(tiny_list2)) # 2258502008448 2258468992832












网友评论