这儿主要记录:根底内容的公共操作,
运算符:
兼并:
"""字符串"""
str1 = 'aa'
str2 = 'bb'
str3 = str1 + str2
print(str3) # aabb
"""列表"""
l1 = [1, 3]
l2 = [10, 20]
l3 = l1 + l2
print(l3) # [1, 3, 10, 20]
"""元组"""
t1 = (1, 3)
t2 = (10, 30)
t3 = t1 + t2
print(t3) # (1, 3, 10, 30)
复制:
"""字符串"""
print('-' * 10) # ----------
"""列表"""
l4 = ['hello']
print(l4 * 4) # ['hello', 'hello', 'hello', 'hello']
"""元组"""
t4 = ('world',)
print(t4 * 4) # ('world', 'world', 'world', 'world')
in 或 not in
"""字符串"""
print('a' in 'abcd') # True
print('a' not in 'abcd') # False
"""列表"""
l5 = ['a', 'b', 'c', 'd']
print('a' in 'abcd') # True
print('a' not in 'abcd') # False
"""元组"""
t5 = ('a', 'b', 'c', 'd')
print('aa' in t5) # False
print('aa' not in t5) # True
可变与不行变数据类型:
-
可变:列表、字典、调集 (内存地址前后不发生改变)
-
不行变:数值、字符串、元组 (内存地址前后发生改变)
公共方法:
max():
"""字符串之间的巨细比较:比较的是ascll码值"""
a = 'abcdefg'
b = 'bcdefg'
print(max(a)) # g
li = [1, 3, 5, 7]
print(max(li)) # 7
print(min(li)) # 1
min():用法同上
enumerate() 枚举,可遍历方针
"""enumerate() 枚举,可遍历方针"""
li_2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i, j in enumerate(li_2):
print(i, j)
"""enumerate(可遍历方针,start = 0)"""
# (0, 'a')
# (1, 'b')
# (2, 'c')
# (3, 'd')
li_3 = ['a', 'b', 'c', 'd']
for i in enumerate(li_3):
print(i)
for index, char in enumerate(li_3):
print(f"下标是{index},对应的字符是{char}")
高阶函数:zip()
zip()函数,用于将可迭代的方针作为参数,将方针中对应的元素打包成一个个元组,然后回来由这些元组组成的列表。
假如各个迭代器的元素个数不一致,则回来列表长度与最短的方针相同,使用*号操作符,可以将元组解压为列表。
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
list_3 = [4, 5, 6, 7, 8]
zipped = zip(list_1, list_2) # 压缩
print(zipped) # <zip object at 0x102fe4280>
print(list(zipped)) # 转为列表:[(1, 4), (2, 5), (3, 6)]
zipped_1 = zip(list_1, list_3)
print(zipped_1) # <zip object at 0x1043c8380>
print(list(zipped_1)) # [(1, 4), (2, 5), (3, 6)]
"""一起遍历多个字典"""
dict_1 = {'name': 'maria', 'age': '30', 'gender': 'girl'}
dict_2 = {'name': 'manta', 'age': '18', 'gender': 'boy'}
for (k1, v1), (k2, v2) in zip(dict_1.items(), dict_2.items()):
print(k1, '-->', v1)
print(k2, '-->', v2)
""" zip(*zipped)中的 *zipped参数,可以list数组,也可所以zip()函数回来的方针。"""
nums = [[1, 2, 3], [4, 5, 6]]
iters = zip(*nums)
print("type of iters is %s" % type(iters)) # type of iters is <class 'zip'>
print(iters) # <zip object at 0x104f3cc40>
m = [1, 2, 3]
n = [4, 5, 6]
print("*zip(m,n)回来:", *zip(m, n)) # *zip(m,n)回来: (1, 4) (2, 5) (3, 6)
""" *zip()函数是zip()函数的逆进程,将zip方针变成原先组合前的数据 """
m2, n2 = zip(*zip(m, n))
print("m2 和 n2 的值分别是:", m2, n2) # m2 和 n2 的值分别是: (1, 2, 3) (4, 5, 6)
对多个元素一起进行排序:
"""姓名与分数一一对应进行排序,又不损坏对应关系"""
names = ['Tom', 'Bob', 'Dave']
scores = [90, 80, 100]
data = list(zip(names, scores))
data.sort() # 优先对姓名进行排序
print(data) # [('Bob', 80), ('Dave', 100), ('Tom', 90)]
对数据进行计算:
profit = 0 # 赢利
sales = [52000, 51000, 49000] # 销售额
cost = [46800, 45900, 43000] # 成本
# 赢利:
for sales, cost in zip(sales, cost):
profit = sales - cost
print(profit)
构建字典:
"""将上面的成果兼并起来,得到一个字典:"""
stu = dict(zip(names, scores))
print(stu) # {'Tom': 90, 'Bob': 80, 'Dave': 100}
容器类型的互转:
list 列表
tuple 元组
set 调集
li = [1, 3, 5, 7]
tup = (1, 2, 34, 5, 7, 7, 7)
s1 = {1, 2, 34, 5, 6}
print(list(tup), type(list(tup))) # 元组转列表 [1, 2, 34, 5, 7, 7, 7] <class 'list'>
print(set(tup), type(set(tup))) # 元组转调集 {1, 2, 34, 5, 7} <class 'set'>
# print(tup(li), type(tup(li))) # 列表转元组 # “元组”方针不行调用
print(set(li), type(set(li))) # 列表转调集 {1, 3, 5, 7} <class 'set'>
print(list(s1), type(list(s1))) # 调集转列表 [1, 2, 34, 5, 6] <class 'list'>
# print(tup(s1), type(tup(s1))) # 调集转元组 # “元组”方针不行调用
推导式:
-
推导式:都是对可变数据类型来使用的
-
什么是遍历:从序列中一个一个取出元素的进程
-
什么是迭代:通常用于循环、递归,重复履行某个操作来逐步逼近方针的进程
-
列表推导式
[表达式 for 变量 in 列表 if 条件]
-
字典推导式
{键:值 for 变量 in 字典.items() if 条件}
-
调集推导式
{表达式 for 变量 in 调集 if 条件}
-
元组推导式
(表达式 for 变量 in 元组 if 条件)
列表推导式:
1、for循环:
# for循环完成进程:
li = []
for i in range(1, 11):
li.append(i / 2)
print(li) # [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
# 列表推导式完成进程:
new_li_1 = [i / 2 for i in range(1, 11)]
print(new_li_1) # [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
2、带 if 的列表推导式:
# 使用步长完成:
li_2 = []
for i in range(0, 10, 2):
print(i, end=" ") # 0 2 4 6 8
li_2.append(i)
print(li_2) # [0, 2, 4, 6, 8]
new_li_2 = [i for i in range(0, 10, 2)]
print(new_li_2) # [0, 2, 4, 6, 8]
# if 完成:
li_3 = []
for i in range(1, 11):
if i % 2 == 0:
print(i)
li_3.append(i)
print(li_3) # [2, 4, 6, 8, 10]
li_4 = [2, 4, 6, 8, 10]
print(li_4)
"""回来的值 循环表达式 条件判别"""
new_li_4 = [i for i in range(1, 11) if i % 2 == 0]
print(new_li_4) # [2, 4, 6, 8, 10]
字典推导式:
1、创立一个字典:
dic1 = {i: i ** 2 for i in range(1, 6)}
print(dic1) # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
2、将2个列表变为一个字典:
li_5 = ['name', 'age', 'sex']
print(len(li_5)) # 3
li_6 = ['Tom', '20', 'boy']
dic2 = {li_5[i]: li_6[i] for i in range(len(li_5))}
print(dic2) # {'name': 'Tom', 'age': '20', 'sex': 'boy'}
3、提取字典中的方针数据:
"""需求:提取上述电脑数量大于等于200的字典数据"""
cumputer = {'AUC': 260, 'HP': 125, "DELL": 208, 'Lenovo': 180}
count = {k1: v1 for k1, v1 in cumputer.items() if v1 >= 200}
print(count) # {'AUC': 260, 'DELL': 208}
"""等同于:"""
for key, value in cumputer.items():
if value >= 200:
count[key] = value
print(count) # {'AUC': 260, 'DELL': 208}
调集推导式:
1、计算平方:
new_set = {i ** 2 for i in (1, 3, 5)}
print(new_set) # {1, 9, 25}
2、输出非abd的字母:
"""调集:无序、去重"""
a = {x for x in 'dfadfnamdjk' if x not in 'abd'}
print(a) # 回来的是生成器方针
print(tuple(a)) # 直接将生成器方针转换为元组
元组推导式:
生成包括1-9数字的元组:
a = (j for j in range(1, 10))
print(a) # 回来的是生成器方针 <generator object <genexpr> at 0x104ccc040>
print(tuple(j for j in range(1, 10))) # (1, 2, 3, 4, 5, 6, 7, 8, 9)