面对洋洋洒洒的知识点,我往往 “一看就会,一写就废”,为了更有针对性的加深知识点的印象,我将以做题的形式继续总结 Python 系列,每篇 10 道问答,以下是本篇目录:
Python 系列总结都是我自己平时的学习笔记,如果有不正确的地方,希望各位佬儿哥指正纠偏
答:
s = "Python is a programming language. Python is awesome"
# 字符类型是不可变数据类型,str.replace()方法不会改变原字符串,会生成新值
ss = s.replace('P','p')
print(ss)
a = 'python is a programming language. python is python.'
答:
# 我得笨方法:
d = {}
for _ in a:
if _ not in d:
d[_] = 1
else:
d[_] += 1
print(d['p'])
# 标答
print(a.count('p'))
a = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
答:
b = [list(s) for s in zip(*a)]
print(b)
斐波那契数列是一个数列,其中的每个数都是前两个数的和。
答:
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0, ]
elif n == 2:
return [0, 1]
else:
fib = [0, 1]
for i in range(2, n):
fib.append(fib[-1] + fib[-2])
return fib
print(fibonacci(10))
-----------------------------------------------------
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
def bubble(l):
for i in range(len(l) - 1):
for j in range(len(l) - 1 - i):
if l[j] > l[j + 1]:
l[j], l[j + 1] = l[j + 1], l[j]
return l
答:解包是 Python 里的一种特殊赋值语句,可以把可迭代对象 (列表,元祖,字典,字符串) 一次性赋给多个值。
a, b, c = (1, 2, 3)
a, b = b, a
numbers = [1, 2, 3, 4, 5]
for a, b in zip(numbers, numbers[1:]):
print(a, b)
---------------------------------------------------------
1 2
2 3
3 4
4 5
注意:
users = {'tom': 19, 'jerry': 13, 'jack': None, 'andrew': 43}
答:
知识点:在排序前将年龄为 None 的值变更为正无穷大;
# 我想到的方法:
# 先将字典中年龄为None的值变更成正无穷大
def key_func(users):
for key in users.keys():
if users[key] is None:
users[key] = float('inf')
# 根据年龄进行正序排序,最后以列表形式输入排序好的姓名
def sort_user(user: dict):
sort_item_for_v = sorted(user.items(), key=lambda x: x[1])
return [user[0] for user in sort_item_for_v]
key_func(users)
print(sort_user(users))
-------------------------------------------------------------------------------
['jerry', 'tom', 'andrew', 'jack']
# 参考答案:
def sort_user_inf(users: dict):
"""
接收一个key为姓名,value为年龄的字典,根据年龄正序排列姓名,若没有年龄,则放在最后
:param users: 用户名:年龄字典
:return: 返回姓名列表
"""
def key_func(username):
age = users[username]
# 当年龄为空时,返回正无穷大作为key,因此就会被排到最后
return age if age is not None else float('inf')
return sorted(users.keys(), key=key_func)
print(sort_user_inf(users))
-----------------------------------------------------------------------------
['jerry', 'tom', 'andrew', 'jack']
答:
d1 = {'name': 'Lili', 'score': 90}
d2 = {'name': 'Tom', 'hobby': 'run'}
d1.update(d2)
print(d1)
-----------------------------
{'name': 'Tom', 'score': 90, 'hobby': 'run'}
# 这种方法会改变d1的原始值
d1 = {'name': 'Lili', 'score': 90}
d2 = {'name': 'Tom', 'hobby': 'run'}
def update_dict(d1: dict, d2: dict):
d = d1.copy()
d.update(d2)
return d
print(update_dict(d1,d2))
print(d1)
print(d2)
---------------------------------------
{'name': 'Tom', 'score': 90, 'hobby': 'run'}
{'name': 'Lili', 'score': 90}
{'name': 'Tom', 'hobby': 'run'}
d1 = {'name': 'Lili', 'score': 90}
d2 = {'name': 'Tom', 'hobby': 'run'}
print({**d1, **d2})
print({**d2, **d1})
print(d1)
print(d2)
--------------------------------------
{'name': 'Tom', 'score': 90, 'hobby': 'run'}
{'name': 'Lili', 'hobby': 'run', 'score': 90}
{'name': 'Lili', 'score': 90}
{'name': 'Tom', 'hobby': 'run'}
d1 = {'name': 'Lili', 'score': 90}
d2 = {'name': 'Tom', 'hobby': 'run'}
print(d1|d2)
print(d2|d1)
print(d1)
print(d2)
-----------------------------
{'name': 'Tom', 'score': 90, 'hobby': 'run'}
{'name': 'Lili', 'hobby': 'run', 'score': 90}
{'name': 'Lili', 'score': 90}
{'name': 'Tom', 'hobby': 'run'}
答:
第一种类型:
s = '1,2,3,4,5'
l = s.split(',')
print(l)
l2 = [int(i) for i in l]
print(l2)
--------------------
['1', '2', '3', '4', '5']
[1, 2, 3, 4, 5]
第二种类型:
l1 = ['1','2','3']
s1 = ','.join(l1)
print(s1)
---------------
1,2,3
第三种类型:
l2 = [1,2,3]
s2 = ','.join([str(i) for i in l2])
print(s2)
-----------------------------
1,2,3