整理和补充
- int
- py2/py3
- 除法
- 强制转换:
- int(‘字符串’) 【重要】
- int(布尔值)
- bool
- 强制转换:
- bool(整数) -> bool(1)/… -> bool(0)
- bool(字符串) -> bool(‘xx’) --> bool("")
- bool(列表) -> bool([]) --> bool([])
- bool(元组) -> bool(()) --> bool(空元组)
v1 = bool(0)
v2 = bool("")
v3 = bool([])
v4 = bool(())
print(v1,v2,v3,v4) # False
- 强制转换:
- str
- 独有功能:upper/lower/split/strip/replace/isdigit/startswith/endswith/format/join/encode
- 公共公共:
- len
- 索引
- 切片
- 步长
- for循环
- 删除【无】
- 更新【无】
- 强制转换:
- str(999) # “999”
- str(True) # “True”
- str([“唐开发”,‘李忠伟’]) # “[“唐开发”,‘李忠伟’]” --> v2 = “”.join([“唐开发”,‘李忠伟’])
- str([“唐开发”,‘李忠伟’])
- list
- 独有功能:append/insert/pop/remove/clear/ extend
- 公共功能:
- len
- 索引
- 切片
- 步长
- for循环
- 删除
- 更新
- 强制转换:
- list(“asdfadfasfdadfasfd”)
v1 = list(“asdfadfasfdadfasfd”)
print(v1) - list( (11,22,33,44,) )
v1 = list( (11,22,33,44,) )
print(v1)
- list(“asdfadfasfdadfasfd”)
- tuple
- 独有功能【无】
- 公共功能:
- len
- 索引
- 切片
- 步长
- for循环
- 删除【无】
- 更新【无】
- 强制转换:
- tuple(‘adfadfasdfasdfasdfafd’)
v1 = tuple(‘adfadfasdfasdfasdfafd’)
print(v1) - tuple([11,22,33,44])
v1 = tuple([11,22,33,44])
print(v1)
- tuple(‘adfadfasdfasdfasdfafd’)
- 总结
- 常见的类型转换
字符串转数字
数字转字符串
列表转元组
元组转列表
其他转bool时,只有:0 “” [] ()
python字典
一、字典 dict
二、字典的操作方法
1.增
2.删
3.改
4.查
三、字典的嵌套
四、集合(了解)
一、字典 dict
why
列表的缺点:
1,列表如果存储的数据比较多,那么他的查询速度相对慢。
2,列表存储的数据关联性不强。
what
python基础数据类型之一:字典。
python中唯一的映射类的数据,花括号{}括起来,他是以键值对形式存储,每一对键值对以逗号隔开的。
{‘name’: ‘太白金星’, ‘age’: 18, ‘sex’: ‘man’}
ps:
数据类型的划分:
1.容器非容器划分:
容器型数据类型:list,tuple,dict,set。
非容器型数据类型:int str bool
2.可变与不可变划分:
可变(不可哈希hash)的数据类型:list dict,set
不可变(可哈希hash的)的数据类型: int bool str tuple
对数据本身变化:
i = 100
i = 90
print(i)
字典是以键值对形式存储的,
键:不重复的,唯一的,键的数据类型必须是不可变的数据类型。
值:任意数据类型,对象。
字典可以存储大量的键值对数据,
python3.5包括3.5之前:字典是无序的。
python3.6之后,字典变成有序的。
字典的优点:
1.字典存储大量的关系型数据。
2.字典的查询速度非常快。
二、字典的操作方法
1.增
第一种方法
dic = {
‘name’: “wu”,
‘age’: 18,
‘hobby’: ‘180’,
}
dic[‘hight’] = 170
print(dic) # {‘name’: ‘wu’, ‘age’: 18, ‘hobby’: ‘180’, ‘hight’: 170}
第二种方法 setdefault()
键存在则不修改,不存在新增
dic = {
‘name’: “wu”,
‘age’: 18,
‘hobby’: ‘180’,
}
dic.setdefault(“name”, “12”)
print(dic) # {‘name’: ‘wu’, ‘age’: 18, ‘hobby’: ‘180’}
dic = {
‘name’: “wu”,
‘age’: 18,
‘hobby’: ‘180’,
}
dic.setdefault(“name1”, “12”)
print(dic) # {‘name’: ‘wu’, ‘age’: 18, ‘hobby’: ‘180’, ‘name1’: ‘12’}
2.删
pop()
按键删除元素
dic = {
‘name’: “wu”,
‘age’: 18,
‘hobby’: ‘180’,
}
ret = dic.pop(“name1”) # 键不存在 报错
print(ret)
ret = dic.pop(“name1”,None)
print(ret) # None
dic = {
‘name’: “wu”,
‘age’: 18,
‘hobby’: ‘180’,
}
ret = dic.pop(“name”)
print(ret) # wu
print(dic) # {‘age’: 18, ‘hobby’: ‘180’}
popitem()
随机删除字典键值对
由于python3.6以后版本字典变成有序的,所以每次删除都会删除最后一个键值对
dic = {
‘name’: “wu”,
‘age’: 18,
‘hobby’: ‘180’,
}
ret = dic.pop(“name”)
print(ret) # wu
print(dic) # {‘age’: 18, ‘hobby’: ‘180’}
clear()
清空字典
dic = {
‘name’: “wu”,
‘age’: 18,
‘hobby’: ‘180’,
}
dic.clear()
print(dic) # {}
del
dic = {
‘name’: “wu”,
‘age’: 18,
‘hobby’: ‘180’,
}
del dic[‘name’]
print(dic) # {‘age’: 18, ‘hobby’: ‘180’}
del dic[‘name1’] # 会报错
del dic # 删除整个字典
print(dic) # 会报错
3.改
dic = {
‘name’: ‘泰山’,
‘age’: 18,
‘hobby’: ‘wife’,
}
dic[‘age’] = 30
print(dic) # {‘name’: ‘泰山’, ‘age’: 30, ‘hobby’: ‘wife’}
update
dic = {
‘name’: ‘泰山’,
‘age’: 18,
‘hobby’: ‘wife’,
}
dic.update(car=‘逍客’, money=1300)
print(dic) # {‘name’: ‘泰山’, ‘age’: 18, ‘hobby’: ‘wife’, ‘car’: ‘逍客’, ‘money’: 1300}
dic = {“name”: “jin”, “age”: 18, “sex”: “male”}
dic2 = {“name”: “wu”, “weight”: 75}
dic2.update(dic)
将dic里面的键值对覆盖添加到dic2中
print(dic) # {‘name’: ‘jin’, ‘age’: 18, ‘sex’: ‘male’}
print(dic2) # {‘name’: ‘jin’, ‘weight’: 75, ‘age’: 18, ‘sex’: ‘male’}
4.查
1.按键查找
dic = {
‘name’: ‘泰山’,
‘age’: 18,
‘hobby’: ‘wife’,
}
print(dic[‘name’]) # ‘泰山’
print(dic[‘name1’]) # 报错
2.get()
dic = {
‘name’: ‘泰山’,
‘age’: 18,
‘hobby’: ‘wife’,
}
print(dic.get(‘name’)) # ‘泰山’
print(dic.get(‘name1’)) # None
dic = {
‘name’: ‘泰山’,
‘age’: 18,
‘hobby’: ‘wife’,
}
for k,v in dic.items():
print(‘这是键:’, k)
print(‘这是值:’, v)
这是键: name
这是值: 泰山
这是键: age
这是值: 18
这是键: hobby
这是值: wife
三、字典的嵌套
dic = {
‘name_list’: [‘一哥’, ‘二哥’, ‘三哥’, ‘wu’,]
‘barry’: {
‘name’: ‘泰山’,
‘age’: 18,
‘hobby’: ‘wife’,
}
}
1,给这个列表[一哥’, ‘二哥’, ‘三哥’, ‘wu’] 追加一个元素 ‘男孩’。
dic[‘name_list’].append(‘男孩’)
print(dic)
2,将这个列表[‘一哥’, ‘二哥’, ‘三哥’, ‘wu’]中的wu变成首字母大写。
dic[‘name_list’][-1] = dic[‘name_list’][-1].capitalize()
print(dic)
3,将这个键值对 ‘name’: ‘泰山’ 的 ‘泰山’ 改成男神。
dic[‘barry’][‘name’] = ‘男神’
print(dic)
4,给barry对应的小字典增加一个键值对: weight: 160
dic[‘barry’][‘weight’] = 150
print(dic)