"""
主类
"""
import keyword
# ############## 字符串常规操作 ##############
def str_op():
# 字符串
print("字符串" + '''123''')
print("字符串" + "\"789\"")
print("原始字符串" + r"\"567\"")
# format
name = '峰哥'
age = 33
format_string1 = f"我的名字是{name},年龄是{age}"
print(format_string1)
format_string2 = 'Hello,{0},成绩提升了{1:.2f}%'.format('小明', 17.125)
print(format_string2)
# 如有字符串mystr = 'hello world itcast and itcastcpp'
mystr = 'hello world itcast and itcastcpp'
# find & rfind:检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1
print(mystr.find('it', 0, 15)) # 12
# index & rindex:同find,但是找不到会抛异常
print(mystr.index('it', 0, 15)) # 12
# count:返回 str在start和end之间 在 mystr里面出现的次数
print(mystr.count('it')) # 2
print(mystr.count('it', 0, 15)) # 1
# replace:把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.
print(mystr.replace('it', 'AA', mystr.count('it'))) # hello world AAcast and
# capitalize:把字符串的第一个字符大写
print(mystr.capitalize())
# title:把字符串的每个单词首字母大写
print(mystr.title())
# lower:转换 mystr 中所有大写字符为小写
# upper:转换 mystr 中的小写字母为大写
print(mystr.lower())
print(mystr.upper())
# startswith:检查字符串是否是以 "hello" 开头, 是则返回 True,否则返回 False
# endswith:检查字符串是否以"world"结束,如果是返回True,否则返回 False.
print(mystr.startswith('hello'))
print(mystr.endswith('world'))
# ljust & rjust:返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
# center:返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
print(mystr.ljust(20))
print(mystr.center(20))
# lstrip & rstrip & strip:删除 mystr 左边/右边/两边的空白字符或指定字符
print(mystr.lstrip())
print(mystr.lstrip('#'))
# split:以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串
# splitlines:按照行分隔,返回一个包含各行作为元素的列表
print(mystr.split(" ", 2)) # ['hello', 'world', 'itcast and itcastcpp']
print(mystr.splitlines())
# partition & rpartition:把mystr以str分割成三部分,str前,str和str后
print(mystr.partition('it')) # ('hello world ', 'it', 'cast and itcastcpp')
# join:alist 中每个元素用str连接,构造出一个新的字符串
alist = ['1', '2', '3']
print("_".join(alist))
# isalpha & isdigit & isalnum & isspace:如果 mystr 所有字符都是字母/数字/字母或数字/空格 则返回 True,否则返回 False
print(mystr.isalpha())
# ############## 列表(List) ##############
def list_op():
order_list = [1, 2, 3]
# 向列表末尾添加元素
order_list.append(4)
# 可以将另一个列表中的元素逐一添加到列表中
order_list.extend([5, 6])
# 在指定位置index前插入元素object
order_list.insert(1, 10)
print(order_list) # [1, 10, 2, 3, 4, 5, 6]
# 修改元素
order_list[1] = 11
print(order_list) # [1, 11, 2, 3, 4, 5, 6]
# 查找元素
print(order_list.index(1, 0, 10)) # 0
# 删除元素
# 根据下标进行删除
del order_list[1]
print(order_list) # [1, 2, 3, 4, 5, 6]
# 按索引删除一个元素,删除时会返回被删除的元素
print(order_list.pop()) # 6
print(order_list) # [1, 2, 3, 4, 5]
print(order_list.pop(4)) # 5
print(order_list) # [1, 2, 3, 4]
# 根据元素的值进行删除,删除第一个符合条件的值
order_list.remove(2)
print(order_list) # [1, 3, 4]
# 元素个数
print(len(order_list))
# 排序
order_list.sort(reverse=True)
print(order_list) # [4, 3, 1]
# 遍历
# 遍历 - for循环
for index in order_list:
print(index)
name4 = [{"hyq": 1, "zlp": 2},
{"hyq": 3, "zlp": 6},
{"hyq": 4, "zlp": 9}]
for index in name4:
print(index['hyq'])
# 遍历 - while循环
i = 0
while i < len(order_list):
print(order_list[i])
i += 1
# 交集 - 【慢】遍历第一个列表的元素看是否存在于第二个列表中
list1 = [1, 2, 3, 4]
list2 = [2, 3, 4, 5]
res = [v for v in list1 if v in list2]
print(res)
# 交集 - 【快】将列表转换为集合并且使用集合操作符得到两个集合的交集, 然后再转换为列表类型
res = list(set(list1) & set(list2)) # [2, 3, 4]
res = set(list1).intersection(set(list2)) # {2, 3, 4}
print(res)
# 并集
res = list(set(list1).union(set(list2)))
print(res)
# 差集
res = list(set(list1).difference(set(list2)))
print(res)
# ############## 元祖(tuple) ##############
def tuple_op():
tuple_1 = (1, 2, 3, "liupeng", "demo")
# 通过索引更新 -> (1, 3, 'demo')
tuple_2 = tuple_1[0], tuple_1[2], tuple_1[4]
print(tuple_2)
# 通过切片更新 -> (1, 2)
tuple_3 = tuple_1[0:2]
print(tuple_3)
# 添加元素 -> (1, 2, 3, 'liupeng', 'demo', 4)
tuple_4 = tuple_1 + (4,)
print(tuple_4)
# ############## 字典(dict) ##############
def dict_op():
# 添加元素
tuple_1 = (1, 2, 3)
# 空字典{}
book = {"book1": "书1", "book2": 2, tuple_1: "书3"}
print(book)
# 在使用 变量名['键'] = 数据 时,这个“键”在字典中,不存在,那么就会新增这个元素
book["test"] = "test"
# 删除元素
# 按键删除一个元素,删除时会返回被删除元素的值
popValue = book.pop("book2")
print(popValue)
# 删除指定键
del book["book1"]
print(book)
# 删除整个字典对象
# del book
# 清空整个字典内容
# book.clear()
# print(book)
# 修改元素
# 只要通过key找到,即可修改
book["book1"] = "test"
print(book)
# 查询元素
# 通过键访问值
print(book["book1"])
# 在我们不确定字典中是否存在某个键而又想获取其值时,可以使用get方法,还可以返回默认值
print(book.get("test"))
# 若info中不存在'age'这个键,就返回默认值18
print(book.get("haha", 18))
# 遍历
# 遍历key
di = {'Michael': 95, 'Bob': 75}
for key in di.keys():
print(key)
# 遍历value
for value in di.values():
print(value)
# 遍历项(元素)
for item in di.items():
print(item)
# 遍历项(元素)
for k, v in di.items():
print(f'kkkkkkk:{k}')
print(f"vvvvvvv:{v}")
# 字段长度
print(len(di))
# ############## 集合(set) ##############
def set_op():
a = set('abc')
# 添加元素
a.add('d')
print(a)
a = {'a', 'b', 'c'}
print(a)
# 删除元素
a.remove('b')
print(a)
# 交集
s1 = {'lp', 'jj'}
s2 = {'jj'}
print(s1 & s2) # jj
# 并集
print(s1 | s2)
print(s1.union(s2))
# 差集
print(s1 - s2)
print(s1.difference(s2))
# 对称差集 - 集合A与集合B中所有不属于A∩B的元素的集合
s1 = {"lp", "xq"}
s2 = {"lp"}
print(s1 ^ s2) # {'xq'}
# ############## 高级特性 ##############
def high_op():
test = [x * x for x in [2, 5]]
print(test) # [4, 25]
test = [x * x for x in [2, 5] if x % 2 == 0]
print(test) # [4]
test = [x for x in [2, 5] if x in [5, 6]]
print(test) # [5]
test = [x + y for x in [2, 5] for y in [3, 6]]
print(test) # [5, 8, 8, 11]
# ############## yield关键字 ##############
def get_value(n):
for i in range(n):
print('