{}
占位符:1. 顺序
"{}, {}".format(s1, s2)
2. 索引
"{2}, {1}".format(s1, s2)
3. key
"{p1}, {p2}".format(p1=s1, p2=s2)
str = format(num, '格式')
case:
format(123.45, '0.2f')
format(123456.78, ',') #千分位分隔符
format(123456.78, "0,.2f") #千分位分隔符
{:格式}
"{}, {:0,.3f}".format(s, num)
%
"%s, %d, %.2f" % (s, num1, num2)
str.split(",")
成员运算符 in / not:在指定序列中查找
身份运算符 is / is not: 判断两个变量是否引用自同一对象(比较内存地址:是否指向同一内存空间)
运算符 | 作用 |
---|---|
& | 与 |
| | 或 |
^ | 异或 |
~ | 取反 |
<< | 左移 |
>> | 右移 |
四种基本数据结构:
列表(list)
元组(Tuple)
字典(Dictionary)
集合(Set)
索引:
正序:0
->
倒序:<- -1
切片:范围取值
[start : end : step_len]
.index(e) # 第一次匹配的索引
.reverse()
lst.reverse()
print(lst)
lst.sort()
print(lst)
lst.sort(reverse=True) # 降序排序
print(lst)
list.append(new_item)
list.insert(index, new_item)
list[index] = new_item
list[start_index:end_index] = new_list
list.remove(item)
list.pop(index)
.count(element)
.extend(new_lst)
成员运算符 in / not in 判断是否在列表中
创建副本
区别:
new_lst = lst. copy() # new_lst is not lst
new_lst = lst # new_lst is lst,简单的引用(指向原空间)
.clear()
lst = ['fjj', 'hp', 'tq', 'yxy']
print(lst[0])
print(lst[-4])
print(lst[:3:2])
print(lst[::2])
print(lst[1::3])
print(lst[-1: -3: -1])
print(lst[-3: -1: 1])
print(lst.index('fjj'))
lst.reverse()
print(lst)
lst.sort()
print(lst)
lst.sort(reverse=True) # 降序排序
print(lst)
lst[1:3] = lst[2:0:-1]
print(lst)
lst[1:3] = []
print(lst)
print('yxy' not in lst)
列表在表达结构化数据(key: value,有明确属性) 时语义不明确,
python中的内置数据结构
字典可修改,运行时动态调整存储空间
{‘key’ : value}
dict(key=value) (key默认字符串,不需要加’’)
通过列表创建:
dict.fromkeys(key列表, [默认初始值,缺省为None])
dict1 = {
'name' : 'xxx',
'height' : 175,
'weight' : 80
}
print(dict1)
dict2 = dict(name='xxx', height=175, weight=80)
print(dict2)
dict3 = dict.fromkeys(['fjj', 'hp', 'yxy'], 'N/A')
print(dict3)
[]
get(key, [若不存在返回的结果,缺省为None])
print(dict1.get('name'))
print(dict1.get('sex'))
print(dict1.get('sex', 'F'))
for key in dict1:
value = dict1[key]
print(key, value)
for key, value in dict1.items():
print(key, value)
[] = new_value
批量更新:update(key1=new_v1, key2 = new_v2)
dict1.update(sex='F', height=190)
删除:
pop(key)
kv = popitem() 删除最后一个kv
clear() : 清空字典
dict1.pop('name')
print(dict1)
kv = dict1.popitem()
print(kv)
dict1.clear()
print(dict1)
setdefault(key, value)
keys(): 获取所有的键
values(): 获取所有值
items():获取所有键值对
返回的都是包装过的list(dict_):原因 -> 字典的视图对象,随着字典的类型随时发生变化
利用字典格式化字符串:
老版本:
print("%(name)s, %(height)d, %(weight)d" % dict1)
新版本:
print("{name}, {height}, {weight}".format_map(dict1))
数字指纹
hash(_obj) : 将任一对象映射到一个hash值
元组:不可变的列表 () # 括号可省略
元素创建后不可修改,但可以通过 元组运算符 来创建新的元组
写:本身元素不可修改
特殊情况:元组中包含列表,列表可修改
t = ([0, 1, 2], [3, 4, 5])
t[0].append(8)
print(t)
t[0][1] = 99
print(t)
元组运算符:同样适用于列表
print([0] * 5)
+:内存开辟新空间,放入原来的两个元组元素
*n:重复n次
[特殊] 如果元组中只有一个元素时,必须在其后添加,
说明其为一个元组,否则会被当做普通变量
list | tuple |
---|---|
允许扩展 | 内容不可变 |
内存存储动态变化 | 创建后固定不变 |
效率较低 | 效率最高 |
运行时数据需要变更时使用 | 用于保存稳定不变的数据 |
保存天气数据、股市数据 | 保存国家名、元素周期表 |
列表:内存连续空间
有序的队列: 有序 + 索引
包含:字符串,列表,元组,数字序列(Range)
内容不可变
range(start, end, [step_len])
tuple(list): 列表 -> 元组
str():将单个变量转化为字符串
join(): 对列表元素进行连接, “元素间的分隔符”.join([‘a’, ‘b’, ‘c’])
【注】必须要求所有元素都是字符串
没有Value的字典,使用{}
交(Intersection)
set3 = set1.intersection(set2)
set1.intersection_update(set2)
并(Union)
set3 = set1.union(set2)
差(Difference)
set3 = set1.difference(set2)
set1.difference_update(set2)
异或:
set3 = set1.symmetric_differece(set2)
set1.symmetric_differece_update(set2)
==:
包含:
set1.issubset(set2) # 子集
set2.issuperset(set1) # 父集
集合间是否有重复(交集)
set1.isdisjoint(set2) # True不存在, False存在
增:
.add():一次只能添加一个元素
.update():一次可添加多个元素(list)
删:
.remove(): 删除不存在的元素会报错
.discard(): 删除不存在的元素则跳过
语法:
[被追加的数据 循环语句(外层) 循环(内层)或判断语句] | {}
lst = [i for i in range()]
lst2 = [i for i in range() if i % 2 == 0]
==>
for i in range():
if i % 2 == 0:
lst2.append(i)
dict = {i+1:lst[i] for i in range(len(lst))}
set = {i * j for i in range(4) for j in range(4) if i == j}