? ? ? ?主要内容是元组、字典和集合的学习。
? ? ? 因为本人学习时比较偏重实践,学习一个新知识后希望能够自主举出实用例子,对应的练习和注释都在代码中呈现,故日志主体主要通过代码来展现。
# -*- coding: utf-8 -*-
# ==========CloudyTenderness===========
"""
@Author 欢迎
@WELCOME CloudyTenderness
@Date 2021/9/27 14:26
@Describe 元组
@version 1.0
"""
new_tuple = ('a', 'b', 1, "哈哈哦吼") # 直接 = 赋值的元组
print(new_tuple)
print(type(new_tuple)) # print type
new_tuple = (3,) # 包含一个元素的元组,最后必须多写个逗号
print(new_tuple)
print(type(new_tuple)) # print type
new_tuple = () # empty tuple
print(new_tuple)
print(type(new_tuple)) # print type
print('----------------------------------------------')
# tuple use
new_str = 'afternoon'
new_tuple = tuple(new_str)
print(new_str)
print(type(new_tuple))
a_list = [1, 2, 4, 5]
new_tuple = tuple(a_list)
print(new_tuple)
print(type(new_tuple))
s = tuple() # 空元组
print(s)
# del 能删除元组,但不能删除其中的元素
# -*- coding: utf-8 -*-
# ==========CloudyTenderness===========
"""
@Author 欢迎
@WELCOME CloudyTenderness
@Date 2021/9/27 15:02
@Describe 集合
@version 1.0
"""
new_set1 = {'ii', 'oo', 'pp', 4}
new_set = set(range(2, 8))
print(new_set)
new_set = set([2, 7, 'dg,eb'])
print(new_set)
new_set = {} # 空集合
new_set = {1, 2, 3, 4, 5, 9}
new_set.pop() # 去除当前第一个元素
print("after pop:", new_set)
new_set.add(204) # 增加204这个元素
print("after add:", new_set)
new_set.remove(9) # 移除9这个元素
print("after remove:", new_set)
print("并集:", new_set | new_set1) # 两个集合并集
print("交集:", new_set & new_set1)
print("差:", new_set - new_set1)
print("对称差集:", new_set ^ new_set1)
a = {1, 2}
b = {1, 2, 3, 4}
c = {4, 67, 902}
print(c > a)
print(b > a)
# -*- coding: utf-8 -*-
# ==========CloudyTenderness===========
"""
@Author 欢迎
@WELCOME CloudyTenderness
@Date 2021/9/27 14:39
@Describe 字典
@version 1.0
"""
a_dict = {'user': 'littleDog',
'age': 18,
'love': 'cat',
}
print(a_dict)
a_dict = {} # 空字典
print(a_dict)
a = [1, 2, 3, 4]
b = ['saf', 'bb', 'ccc', 'all']
print(a_dict.get('user', 'littleDog'))
c = dict(zip(a, b))
print(c)
d = dict() # 空字典
new_dict = dict.fromkeys(['name', 'age', 'food']) # dict.fromkeys 初始化,且value对应为空
print(new_dict)
print("-------------------------------------------------------------")
a_dict = {'user': 'littleDog',
'age': 18,
'love': 'cat',
}
print(a_dict.get('user'))
print(a_dict.get('littleDog'))
print("-----item way:")
for item in a_dict.items():
print(item)
print("-----key way:")
for key in a_dict:
print(key)
print("-----序列解包:")
for key, value in a_dict.items(): # 序列解包用法
print(key, value)
print("-----key():", a_dict.keys())
print("-----values():", a_dict.values())
print("-------------------------------------------------------------")
a_dict.update({'a': 'a', 'b': 'b'}) # update修改
print(a_dict)
a_dict['user'] = 'Cat' # 字典修改
print(a_dict)
a_dict['name'] = 'me!' # 字典增加
print(a_dict)
还有一部分推导式和解包的学习
# -*- coding: utf-8 -*-
# ==========CloudyTenderness===========
"""
@Author 欢迎
@WELCOME CloudyTenderness
@Date 2021/9/28 21:31
@Describe 推导式
@version 1.0
"""
# 集合推导式
s = {x.strip() for x in (' 你好啊 ', ' 小狗', '真可爱啊! ')}
print(s)
# 字典推导式
s = {i: str(i) for i in range(5, 12)}
print(s)
x = ['A', 'B', 'C', 'D']
y = ['exp', 'money', 'grade', 'win']
s = {i: j for i, j in zip(x, y)}
print(s)
# 生成器推导式
g = ((i) ** 2 for i in range(10)) # 生成0-9的平方
h = ((i + 2) ** 2 for i in range(10))
print("创建生成器对象:", g)
print("list化后的结果: ", list(h))
i = 0
for item in g:
print('i = ', i, "时,对应其平方为:", item)
i += 1
# -*- coding: utf-8 -*-
# ==========CloudyTenderness===========
"""
@Author 欢迎
@WELCOME CloudyTenderness
@Date 2021/9/28 21:17
@Describe 解包操作
@version 1.0
"""
# about list && dict && tuple 记得个数对齐,否则error
new_list = {'gg', 'mm', 'yy'}
new_dict = {'狗狗': 1, '猫猫': 2, '鸭鸭': 3}
new_tuple = (4, 5, 6)
a, b, c = new_dict.items()
print("dict_item()_exp --- a:", a)
print(type(a))
a, b, c = new_list
print("list_exp --- a:", a)
print(type(a))
a, b, c = new_dict
print("dict_exp --- a:", a)
print(type(a))
a, b, c = new_dict.values()
print("dict_values()_exp --- a:", a)
print(type(a))
a, b, c = new_tuple
print("tuple_exp --- a:", a)
print(type(a))
最后是本周的实验指导书对应的实验二
# -*- coding: utf-8 -*-
# ==========CloudyTenderness===========
"""
@Author 欢迎
@WELCOME CloudyTenderness
@Date 2021/9/28 22:05
@Describe python_exp2
@version 1.0
"""
# 1
from functools import reduce
num = input('输入一个自然数:')
print(sum(map(int, num)))
# 2
setA = eval(input('输入一个set:'))
setB = eval(input('再输入一个set:'))
print('交集为', setA & setB)
print('并集为', setA | setB)
print('A-B:', setA - setB)
# 3
num = int(input('输入一个自然数:'))
print("二进制:", bin(num))
print('八进制:', oct(num))
print('十六进制:', hex(num))
# 4
new_list = input('输入一个若干整数的list:')
new_list = eval(new_list)
print(list(filter(lambda x: x % 2 == 0, new_list)))
# 5
list1 = eval(input('input一个包含若干整数的列表 list1:'))
list2 = eval(input('input一个包含若干整数的列表 list2:'))
res = dict(zip(list1, list2))
print(res)
# 6
lst = eval(input('请输入包含若干整数的列表lst:'))
print(sorted(lst, reverse=True))
# 7
lst = eval(input('输入一个包含若干整数的list:'))
print(reduce(lambda x, y: x * y, lst))
# 8
lst1 = eval(input('输入包含2个整数的列表lst1:'))
lst2 = eval(input('输入包含2个整数的列表lst2:'))
print(sum(map(lambda i, j: abs(i - j), lst1, lst2)))
# 9
lstSets = eval(input('请输入包含若干集合的列表:'))
print(reduce(lambda x, y: x | y, lstSets))
# 10
a1 = int(input('请输入等比数列首项:'))
q = int(input('请输入等比数列公比:'))
n = int(input('请输入一个自然数:'))
res = a1 * int('1' * n, q)
print(res)
# 11
data = input('输入一个字符串:')
d = dict()
for ch in data:
d[ch] = d.get(ch, 0) + 1
mostCommon = max(d.items(), key=lambda item: item[1])
print(mostCommon)
|