一、列表
1、定义“在【】内,用逗号分隔开多个任意数据类型的值
x1 = [1,"a",[1,2]] #本质x1 = list([1,"a",[1,2]])
2、类型转换
但凡能被for循环遍历的数据类型都可以传给list()转换成列表类型,list()会跟for循环一样遍历出数据
list("wdad") #结果:【"w","d","a","d"】
list([1,2,3]) #结果: 【1,2,3】
list({"name:","jason","age":18}) #结果:【"name","age"】
3、使用
3.1按索引存取值(正向存取+反向存取),即可存也可取
正向取(从左往右)
l = ["egon","tony","jason"]
print(l(0))
反向取(负号表示从右往左)
l [-1]
对于list来说,即可以按照索引取值,又可以按照索引修改指定位置的值,但如果索引不存在则报错
l = ["egon","tony","jason"]
l [1] = "Anna"
"egon" "Anna" "jason"
3.2切片(顾头不顾尾,步长)
顾头不顾尾,取出索引为0到3的元素
l = ["egon",34,"tom","jason"]
print(l[0:3])
步长:0:3:2,第三个参数2代表步长,会从0开始,每次累加一个2即可,所以会取出索引0、2的元素
l = ["egon",34,"tom","jason"]
print(l[0:3:2])
3.3长度
>>>len(l)
3.4成员运算in和not in
>>>"egon" in l
True
>>>"XXX" not in l
True
3.5添加
append()列表尾部的追加元素
>>> l1 = ["a","b","c"]
>>>l1.append("d")
>>>l1
["a","b","c","d"]
extend()一次性在列表尾部添加多个元素
>>>l1 = ["a","b","c"]
>>>l1.extend("a,b,c")
["a","b","c","a","b","c"]
insert()在指定位置插
>>>l = ["egon",34,"tom","jason"]
>>>l.insert(0,18)
>>>print(l)
[18, 'egon', 34, 'tom', 'jason']
3.6删除:
l = ["egon",34,"tom","jason"]
del l[1] #删除的元素为1,也可直接删除整个列表,直接del + 列表名即可
print(l)
['egon', 'tom', 'jason']
pop()默认删除列表最后一个元素,并将删除的值返回括号内可以通过加索引值来指定删除元素
l = ["egon",34,"tom","jason"]
res=l.pop()
print(l)
['egon', 34, 'tom']
remove()括号内指定删除哪个元素,没有返回值
l = ["egon",34,"tom","jason"]
res=l.remove(34)
print(l)
['egon', 'tom', 'jason']
3.7 reverse()返转列表内元素顺序
l = ["egon",34,"tom","jason"]
l.reverse()
print(l)
['jason', 'tom', 34, 'egon']
3.8 sort()给列表内所有元素排序
1、排序时列表元素之间必须是相同数据类型,不可混搭,否则报错
>>> l = [11,22,3,42,7,55]
>>> l.sort()
>>> l
[3, 7, 11, 22, 42, 55] # 默认从小到大排序
>>> l = [11,22,3,42,7,55]
>>> l.sort(reverse=True) # reverse用来指定是否跌倒排序,默认为False
>>> l
[55, 42, 22, 11, 7, 3]
2、了解知识:我们常用的数字类型比较大小,但其实,字符串、列表等都可以比较大小,原理相同,都是依次比较
3.9 循环
>>>l = ["egon",34,"tom","jason"]
>>>for i in l:
>>print(i)
egon
34
tom
jason
二、元组
1、作用:元组与列表类似,也是可以存多个任意类型的元素,不同之处在于元组的元素不能修改,即元组相当于不可变的
列表,用于记录多个固定不允许修改的值,单纯用于取。
2、定义方式:在()内用逗号分隔开多个任意类型的值
>>>countries = ("中国","美国","英国") #本质countries = tuple("中国","美国","英国")
#强调:如果元组内只有一个值,则必须加一个逗号,否则()就只是包含的意思而非定义元组
3、类型转换:但凡能被for循环的遍历的数据类型都可以传给tuple()转换成元组类型
>>> tuple('wdad') # 结果:('w', 'd', 'a', 'd')
>>> tuple([1,2,3]) # 结果:(1, 2, 3)
>>> tuple({"name":"jason","age":18}) # 结果:('name', 'age')
>>> tuple((1,2,3)) # 结果:(1, 2, 3)
>>> tuple({1,2,3,4}) # 结果:(1, 2, 3, 4)
#tuple()会跟for循环一样遍历出数据类型中包含的每一个元素然后放到元组中
三、字典
1、定义:在{}内用逗号分隔开多个元素,每一个元素都是key:value的形式,其中value可以是任意类型,而key则必须
是不可变类型,通常key应该是str类型,因为str类型对value有描述的功能
info={'name':'tony','age':18,'sex':'male'} #本质info=dict({....})
# 也可以这么定义字典
info=dict(name='tony',age=18,sex='male') # info={'age': 18, 'sex': 'male', 'name': 'tony'}
2、类型转换
>>> info=dict([['name','tony'],('age',18)])
>>> info
{'age': 18, 'name': 'tony'}
fromkeys会从元组中取出每个值当做key,然后与None组成key:value放到字典中
>>> {}.fromkeys(('name','age','sex'),None)
{'age': None, 'sex': None, 'name': None}
3、使用
3.1按key存取值,可存可取
取:
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
print(dic["hobbies"][1])
basketball
对于赋值操作,如果key原先不存在于字典,则会新增key:value
对于赋值操作,如果key原先存在于字典,则会修改对应value的值
3.2 长度len
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
print(len(dic))
3.3 成员运算 in 和 not in
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
print("name" in dic)
3.4删除
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
print(dic.pop("name")) #返回删除的值 egon
3.5 键keys()
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
dic.keys()
print(dic)
值values()
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
dic.values()
键值对items()
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
dic.items()
3.6 循环
默认遍历的是字典的key
for key in dic:
print(key)
只遍在key
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
for key in dic.keys():
print(key)
只遍历value
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
for value in dic.values():
print(value)
遍历key与value
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
for key in dic.items():
print(key)
4、get()
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
print(dic.get("name"))
#key存在,则获取key对应的value值
#key不存 在,可以设置默认返回的值
5、pop() 删除指定的key对应的键对值,并返回值
6、popitem() 随机删除一组键值对,并将删除的键值放到元组内返回
7、update() 用新字典更新旧字典,有则修改,无则添加
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
print(dic.update({"name":"Anna"}))
8、setdafault() #key不存在则新增键值对,并将新增的value返回
dic={"name":"egon","age":18,"hobbies":["play game","basketball"]}
res = dic.setdefault("k1",789)
print(res)
四、集合
1、作用:集合、list、tuple、dict、一样都可以存放多个值,但是集合主要用于:去重、关系运算
2、定义:在{}内用逗号分隔开多个元素,集合具备以下三个特点:
1、每个元素必须是不可变类型
2、集合内没有重复的元素
3、集合内元素无序
3、类型转换:但凡能被for循环的遍历的数据类型,不可变类型都可以传给set()转换成集合类型
4、使用
1、求两个用户所有的好友(重复好友只留一个)
friends1 = {"a","b","c","d"}
friends2 = {"c","d",'e',"b"}
print(friends1|friends2)
2、交集(&)求两个用户的共同好友
3、差集(-)
4、对称差集(^)求两个两户独有的好友们(即去掉共有的好友)
5、值是否相等(==)
五、去重
集合去重复有局限性
1、只能什对不可变类型
2、集合本身是无序的,去重之后无法保留原来的顺序