'''----------------------字典相关函数操作----------------'''
'''------增------'''
# d = {"name":"a","age":18}
# print(d, type(d),id(d)) # {'name': 'a', 'age': 18} <class 'dict'> 2832173858032
# d["height"] = 180
# print(d, type(d),id(d)) # {'name': 'a', 'age': 18, 'height': 180} <class 'dict'> 2832173858032
'''------删------'''
# d = {"name": "a", "age": 18}
# del d["age"]
# print(d) # {'name': 'a'}
# v = d.pop("age")
# print(v, d) # 18 {'name': 'a'}
# 删除按升序排序后的第一个键值对,并以元组的形式返回该键值对;如果字典为空,则报错
# result = d.popitem()
# print(result, d) # ('age', 18) {'name': 'a'}
# print(d.clear(), d) # None {}
'''------改(只能改值,不能改key)------'''
# d = {"name": "a", "age": 18}
# d["age"] = 20
# print(d) # {'name': 'a', 'age': 20}
# d = {"name": "a", "age": 18}
# d.update({"name": "abc", "age": 18, "adress": "四川"})
# print(d) # {'name': 'abc', 'age': 18, 'adress': '四川'}
'''------查询操作------'''
# d = {"name": "a", "age": 18}
# print(d["age"]) # 18
# print(d["age1"]) # KeyError: 'age1'
# print(d.get("age")) # 18
# print(d.get("age1"), d) # None {'name': 'a', 'age': 18}
# v = d.setdefault("age1")
# print(v, d) # None {'name': 'a', 'age': 18, 'age1': None}
# 获取所有值
# d = {"name": "a", "age": 18}
# print(d.values()) # dict_values(['a', 18])
# print(d.keys()) # dict_keys(['name', 'age'])
# print(d.items()) # dict_items([('name', 'a'), ('age', 18)])
'''------遍历------'''
# d = {"name": "a", "age": 18}
# keys = d.keys()
# for key in keys:
# print(d[key])
#
# # 直接遍历所有的键值对
# kvs = d.items()
# for key, value in kvs:
# print(key, value)
'''------计算------'''
# d = {"name": "a", "age": 18}
# print(len(d)) # 2
#
# # 判定,key是否存在
# print("name" in d) # True
# print(18 in d) # False
'''----------------------------集合常用操作------------------'''
'''------单一集合操作------'''
# ------------------------可变集合-----------------------
# 增
# s = {1, 2, 3}
# s.add(4)
# print(s, type(s)) # {1, 2, 3, 4} <class 'set'>
# 删除:
# 1 remove()
# s = {1, 2, 3}
# result = s.remove(3)
# print(result, s) # None {1, 2}
# 2 discard()
# s = {1, 2, 3}
# result = s.discard(3)
# print(result, s) # None {1, 2}
# 3 pop()
# s = {1, 2, 3}
# result = s.pop()
# print(result, s) # {2, 3}
# result = s.pop()
# print(result, s) # 2 {3}
# 4 clear()
# s = {1, 2, 3}
# result = s.clear()
# print(result, s) # None set()
# 修改,元素为不可变类型,不能修改
# 无法通过索引或key进行查询
# 1.通过 for in进行遍历
# 2.通过迭代器进行访问
# --------------------------不可变集合-------------------------
# 不能增删改
# 查:
# 1.通过 for in进行遍历
# 2.通过迭代器进行访问
'''-------------------集合之间的操作------------------'''
# 交集:
# s1 = {1, 2, 3, 4, 5}
# s2 = {4, 5, 6}
# result = s1.intersection(s2)
# print(result, s1, s2) # {4, 5} {1, 2, 3, 4, 5} {4, 5, 6}
# s1 = frozenset([1, 2, 3, 4, 5])
# s2 = {4, 5, 6}
# result = s1.intersection(s2)
# print(result, s1, s2) # frozenset({4, 5}) frozenset({1, 2, 3, 4, 5}) {4, 5, 6}
# result = s2.intersection(s1)
# print(result, s1, s2) # {4, 5} frozenset({1, 2, 3, 4, 5}) {4, 5, 6}
'''注意:可变与不可变集合混合运算,返回结果类型以运算符左侧为主'''
# s1 = frozenset([1, 2, 3, 4, 5])
# s2 = {4, 5, 6}
# result = s1 & s2
# print(result, s1, s2) # frozenset({4, 5}) frozenset({1, 2, 3, 4, 5}) {4, 5, 6}
# s2.intersection_update()会改变s2集合,不可用于不可变集合
# 并集
# s1 = {1, 2, 3, 4, 5}
# s2 = {4, 5, 6}
# result = s1.union(s2)
# print(result, s1, s2) # {1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 5} {4, 5, 6}
# s1 = frozenset([1, 2, 3, 4, 5])
# s2 = {4, 5, 6}
# result = s1.union(s2)
# print(result, s1, s2) # frozenset({1, 2, 3, 4, 5, 6}) frozenset({1, 2, 3, 4, 5}) {4, 5, 6}
# 差集
# s1 = {1, 2, 3, 4, 5}
# s2 = {4, 5, 6}
# result = s1.difference(s2)
# print(result, s1, s2) # {1, 2, 3} {1, 2, 3, 4, 5} {4, 5, 6}
# s1 = {1, 2, 3, 4, 5}
# s2 = {4, 5, 6}
# result = s1 - s2
# print(result, s1, s2) # {1, 2, 3} {1, 2, 3, 4, 5} {4, 5, 6}
# 判定
# s1 = {1, 2, 3, 4, 5}
# s2 = {4, 5, 6}
# print(s1.isdisjoint(s2)) # False,相交
# s1 = {1, 2, 3}
# s2 = {4, 5, 6}
# print(s1.isdisjoint(s2)) # True, 不相交
# s1 = {1, 2, 3, 4, 5}
# s2 = {4, 5, 6}
# print(s1.issuperset(s2)) # False,不包含
# s1 = {1, 2, 3, 4, 5, 6}
# s2 = {4, 5, 6}
# print(s1.issuperset(s2)) # True,包含,s1包含s2
'''---------------------time模块-------------------'''
import time
# result = time.time()
# years = result / (24 * 60 * 60 * 365) +1970
# print(years) # 2023.244657670582
# 获取格式化的时间
# t = time.time()
# result = time.ctime(t)
# print(result) # Sun Mar 19 10:55:42 2023
# time_tuple = time.localtime()
# result = time.asctime(time_tuple)
# print(result) # Sun Mar 19 10:58:16 2023
# 字符串-->格式化日期
# result = time.strftime("%Y-%m-%d %H: %M: %S", time.localtime())
# print(result) # 2023-03-19 11: 01: 10
# result = time.strftime("%y-%m-%d %H: %M: %S", time.localtime())
# print(result) # 23-03-19 11: 02: 03
#
# pt = time.strptime("2023-03-19 11: 02: 03", "%Y-%m-%d %H: %M: %S")
# print(pt) # time.struct_time(tm_year=2023, tm_mon=3, tm_mday=19, tm_hour=11, tm_min=2, tm_sec=3, tm_wday=6, tm_yday=78, tm_isdst=-1)
# print(time.mktime(pt)) # 1679194923.0
# 代码执行时间差
# start = time.perf_counter()
# for i in range(1000):
# print(i)
# time.sleep(1) # 休眠一秒
# end = time.perf_counter()
# print(end - start) # 0.003752200000000011
'''------------------------calendar模块-------------------'''
import calendar
# 获取文本日历
# print(calendar.month(2023, 3))
''' March 2023
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31'''
'''---------------------datetime模块----------------------'''
import datetime
# 获取当天的日期,取出当天日期的年、月、日、时、分、秒
# print(datetime) # <module 'datetime' from 'D:\\Anaconda\\envs\\test1\\lib\\datetime.py'>
# print(datetime.datetime.now()) # 2023-03-19 15:21:59.809969
# print(datetime.datetime.today()) # 2023-03-19 15:22:34.820539
# t = datetime.datetime.now()
# print(t.year) # 2023
# print(t.month) # 3
# print(t.day) # 19
# print(t.hour) # 15
# print(t.minute) # 26
# print(t.second) # 31
import datetime
# 计算n天之后的日期
# t = datetime.datetime.today()
# result = t + datetime.timedelta(days= 7)
# print(t, result) # 2023-03-19 15:31:36.801947 2023-03-26 15:31:36.801947
# 计算两个日期的时间差
# first = datetime.datetime(2023, 3, 26, 16, 00, 00)
# second = datetime.datetime(2023, 3, 27, 16, 00, 00)
# print(second - first) # 1 day, 0:00:00
# print((second - first).total_seconds()) # 86400.0, 取出总秒数