标签:总结 排列 倒序 count 地址 sts 应该 append clear
列表(list) 按照位置存放多个类型的值
在[]内用逗号分隔开多个任意类型的值
l = [111,222,333,‘xxx‘,[11,111,2]] # l =list(...)
把可迭代的类型转换成列表,可以被for循环遍历的类型就是可迭代类型、
res = list (‘hello ‘)
print(res)
>>>[‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘]
res= list({"k1":111,"k2":222})
print(res)
>>>[‘k1‘, ‘k2‘]
取根据索引取值,存是指的覆盖掉原来索引对应的内存地址
l=[111,222,3333]
l[0]=6666 #
# l[3]=7777 #列表索引不能超出范围 否则报错
print(l)
# >>>[6666, 222, 3333]
根据索引切片
l=[11,22,33,44,55,66,77]
l1=l[0:6:2] #起始位置、终点位置、步长
print(l)
print(l1)
>>>[11, 22, 33, 44, 55, 66, 77]
>>>[11, 33, 55]
切片拷贝是输入浅拷贝
l=[11,22,33,44,55,66,77]
print(len(l))
>>>7
print(111 in ["aaa",111,2222])
>>>True
l=[11,22,33,44,55,66,77]
for x in l:
print(x)
# >>>从第一个索引开始循环一遍就赋值给x 一直循环到最后一个元素
for i,x in enumerate(l):
print(i,x)
# >>>0 11
# >>>1 22
# >>>...
#for 赋值变量名 in enumerate (list):
#可以每个值循环前面显示索引
l=[11,22,33,44,55,66,77]
del l[0]
print(l)
# >>>[22, 33, 44, 55, 66, 77]
l=[11,22,33,44,55,66,77]
l.remove(77)
print(l)
>>>[11, 22, 33, 44, 55, 66]
l=[11,22,33,44,55,66,77]
print(l.pop(0))
>>>11
# 对比上面两个pop有返回值 del 和 remove没有返回值
l=[11,22,33,44,55,66,77]
# l.append(值)
print(l.append(88))
>>>None #无返回值
print(l)
>>>[11, 22, 33, 44, 55, 66, 77, 88]
l=[11,22,33,44,55,66,77] #无返回值
l.insert(1,"aa")
print(l)
>>>[11, ‘aa‘, 22, 33, 44, 55, 66, 77]
对应元组6.2
# 拷贝的都是索引 和对应的内存地址 所以浅层拷贝双方不可变类型一方变一方没有影响,但是拷贝的包含可变类型,那么当深层的可变类型发生变化,拷贝双方一起改变
l=[11,22,33,44,33,33,55]
new_l = l.copy() # new_l = l[:]
print(l)
print(new_l)
>>>[11, 22, 33, 44, 33, 33, 55]
>>>[11, 22, 33, 44, 33, 33, 55]
对应元组6.1
l=[11,22,33,44,33,33,55]
print(l.index(55))
>>>6
# l=[11,22,33,44,33,33,55]
l.clear()
print(l)
>>>[]
l=[11,22,33,44,55,33,33,33]
# l.extend() 能被for循环都可以 添加多个值
# l.extend("hello")
# print(l)
# >>>[11, 22, 33, 44, 55, 33, 33, 33, ‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘]
# l.extend([1,2,3])
# print(l)
# >>>[11, 22, 33, 44, 55, 33, 33, 33, 1, 2, 3]
l.extend([[1,2],3])
print(l)
>>>[11, 22, 33, 44, 55, 33, 33, 33, [1, 2], 3]
l=[1,2,3,4,5,6,7]
l.reverse()
print(l)
>>>[7, 6, 5, 4, 3, 2, 1]
# 不是很靠谱的倒序排列 原因如下
l=[11,22,33,44,55,33,33,33]
l.reverse()
print(l)
## >>>[33, 33, 33, 55, 44, 33, 22, 11]
l=[11,22,33,44,55,33,33,33]
l.sort(reverse=True)
print(l)
# # >>>[55, 44, 33, 33, 33, 33, 22, 11]
# l.sort()
# print(l)
# # >>>[11, 22, 33, 33, 33, 33, 44, 55]
# 存一个值or存多个值
# 多个值
# 有序or无序
# 有序
# 可变or不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
# 可变
元组(tuple) 就相当于一种不可变的列表,所以说元组也是按照位置存放多个任意类型的元素
在()内用逗号分隔开多个任意类型的元素
t=(1,2.1,"william",["lili",1,2.1],(2,2.1))
print(type(t))
>>>class ‘tuple‘
注意:如果元组内只有一个元素,必须用逗号分割
t=(1,)
print(type(t))
tuple 转换可迭代的类型
1、按索引取值(正向取+反向取):只能取
t=(11,22,33)
print(t[0]) #正向取
# >>>11
print(t[-1]) #反向取
# >>>33
2、切片(顾头不顾尾,步长)
t=(11,22,33,44,55)
print(t[0:4:2])
# >>>(11, 33)
3、长度
t=(11,22,33,44,55,66,77,88)
print(len(t))
# >>>8
4、成员运算in 和not in
t=(11,22,33,44,55,66,77,88)
print((11) in t)
# >>>True
5、循环
t=(11,11.33,"xxx","xxx","xxx",[22,33])
print(t.count("xxx"))
# >>>3
t=(11,11.33,"xxx","xxx","xxx",[22,33])
print(t.index("xxx"))
# >>>2
print(t.index([22,33]))
# >>>5
# 存一个值or存多个值
# 多个值
# 有序or无序
# 有序
# 可变or不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
# 不可变
字典dict 按照key:value的方式存放多个值,其中key对value应该由描述性的效果
{}内用逗号分隔开多个元素,每个元素都是key:value的组合,其中value可以是任意值,但是key必须是不可变类型,通常是字符串类型
否则key用了可变类型就会报错
# res=dict([("name",‘egon‘),["age",18],[‘gender‘,"male"]])
# print(res)
# >>>{‘name‘: ‘egon‘, ‘age‘: 18, ‘gender‘: ‘male‘}
# res = dict (a=1,b=2,c=3)
# print(res)
# >>>{‘a‘: 1, ‘b‘: 2, ‘c‘: 3}
字典可根据不存在的key 添加key:value
d={"k1":111,‘k2‘:222}
print(d[‘k1‘])
>>>111
d[‘k1‘]=6666
d[‘k3‘]=6666
print(d)
>>>{‘k1‘: 6666, ‘k2‘: 222, ‘k3‘: 6666}
d={"k1":111,‘k2‘:222}
print(len(d))
>>>2
判断的都是key
# d={"name":"egon",‘age‘:18}
# print("name" in d)
# d={"name":"egon",‘age‘:18,"gender":"male"}
# print(d.keys())
# print(d.values())
# print(d.items())
>>>??
dict_keys([‘name‘, ‘age‘, ‘gender‘])
dict_values([‘egon‘, 18, ‘male‘])
dict_items([(‘name‘, ‘egon‘), (‘age‘, 18), (‘gender‘, ‘male‘)])
d={"name":"egon",‘age‘:18,"gender":"male"}
for k in d.keys():
print(k)
for k in d:
print(k)
for v in d.values():
print(v)
for k,v in d.items(): # k,v = (‘gender‘, ‘male‘),
print(k,v)
print(list(d.keys()))
print(list(d.values()))
print(list(d.items()))
d={"name":"egon",‘age‘:18}
print(d)
>>>{‘name‘: ‘egon‘, ‘age‘: 18}
del d["name"]
print(d)
>>>{‘age‘: 18}
d={"name":"egon",‘age‘:18}
v = d.pop("name")
print(d)
>>>{‘age‘: 18}
print(v)
>>>egon
d={"name":"egon",‘age‘:18}
item = d.popitem()#随机删除
print(item)
>>>(‘age‘, 18)
print(d)
>>>{‘name‘: ‘egon‘}
d={"name":"egon",‘age‘:18,"gender":"male"}
print(d["age"])
>>>18
print(d.get("age"))
>>>18
d={"name":"egon",‘age‘:18,"gender":"male"}
d.clear()
print(d)
>>>{}
# d={"name":"egon",‘age‘:18,"gender":"xxx"}
# key不存在则添加key:value,key如果存在则什么都不做
# if "gender" not in d:
# d[‘gender‘]="male"
# d.setdefault("gender","male")
# print(d)
>>>{‘name‘: ‘egon‘, ‘age‘: 18, ‘gender‘: ‘xxx‘}
# d={"name":"egon",‘age‘:18,"gender":"male"}
# d.update({"k1":111,"name":"xxx"})
# print(d)
# >>>{‘name‘: ‘xxx‘, ‘age‘: 18, ‘gender‘: ‘male‘, ‘k1‘: 111}
# 存一个值or存多个值
# 多个值
# 有序or无序
# 无序
# 可变or不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
# 可变
基本数据类型常用操作及内置方法 —(list\tuple\dict)
标签:总结 排列 倒序 count 地址 sts 应该 append clear
原文地址:https://www.cnblogs.com/cnblogswilliam/p/14180934.html