最近为了提升python水平,在网上找到了python习题,然后根据自己对于python的掌握,整理出来了答案,如果小伙伴们有更好的实现方式,可以下面留言大家一起讨论哦~
test = 'hello_world_yoyo'
# 使用split函数,分割字符串,并且将数据转换成列表类型
print(test.split("_"))
结果:
['hello', 'world', 'yoyo']
Process finished with exit code 0
test = ["hello", "world", "yoyo"]
# 使用 join 函数将数据转换成字符串
print("_".join(test))
结果:
hello_world_yoyo
Process finished with exit code 0
这边如果不依赖python提供的join方法,我们还可以通过for循环,然后将字符串拼接,但是在用"+"连接字符串时,结果会生成新的对象,
用join时结果只是将原列表中的元素拼接起来,所以join效率比较高
test = ["hello", "world", "yoyo"]
# 定义一个空字符串
j = ''
# 通过 for 循环 打印出列表中的数据
for i in test:
j = j + "_" + i
# 因为通过上面的字符串拼接,得到的数据是“_hello_world_yoyo”,前面会多一个下划线,所以我们下面把这个下划线去掉
print(j.lstrip("_"))
s = 'We are happy.'
print(s.replace(' ', '%20'))
结果:
We%20are%20happy.
Process finished with exit code 0
for i in range(1, 10):
for j in range(1, i+1):
print('{}x{}={}\t'.format(j, i, i*j), end='')
print()
结果:
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
Process finished with exit code 0
下面是使用while循环实现
i = 1
while i <= 9:
j = 1
while j <= i:
print("%d*%d=%-2d"%(i,j,i*j),end = ' ') # %d: 整数的占位符,'-2'代表靠左对齐,两个占位符
j += 1
print()
i += 1
def test():
message = 'Hello, welcome to my world.'
world = 'welcome'
if world in message:
return message.find(world)
else:
return -1
print(test())
结果:
7
Process finished with exit code 0
def test():
message = 'Hello, welcome to my world.'
# 计数
num = 0
# for 循环message
for i in message:
# 判断如果 ‘w’ 字符串在 message中,则num +1
if 'w' in i:
num += 1
return num
print(test())
def test(str_test, num, counts):
"""
:param str_test: 字符串
:param num: 字符串出现的次数
:param count: 字符串第几次出现的次数
:return:
"""
# 定义一个空数组,存放逻辑处理后的数据
list = []
# for循环字符串的数据
for i in str_test:
# 使用 count 函数,统计出所有字符串出现的次数
count = str_test.count(i, 0, len(str_test))
# 判断字符串出现的次数与设置的counts的次数相同,则将数据存放在list数组中
if count == num:
list.append(i)
# 返回第n次出现的字符串
return list[counts-1]
print(test('gbgkkdehh', 1, 2))
结果:
d
Process finished with exit code 0
def test():
message = 'welcome to my world'
world = 'world'
if world in message:
return True
return False
print(test())
结果:
True
Process finished with exit code 0
def test():
message = 'hi how are you hello world, hello yoyo !'
world = 'hello'
return message.find(world)
print(test())
结果:
15
Process finished with exit code 0
def test(string, str):
# 定义 last_position 初始值为 -1
last_position = -1
while True:
position = string.find(str, last_position+1)
if position == -1:
return last_position
last_position = position
print(test('hi how are you hello world, hello yoyo !', 'hello'))
结果:
28
Process finished with exit code 0
while True:
try:
# 判断输入是否为整数
num = int(input('输入一个整数:'))
# 不是纯数字需要重新输入
except ValueError:
print("输入的不是整数!")
continue
if num % 2 == 0:
print('偶数')
else:
print('奇数')
break
结果:
输入一个整数:100
偶数
Process finished with exit code 0
def test():
user_input = input("请输入您的姓名:")
if '王' in user_input:
return "用户姓王"
return "用户不姓王"
print(test())
结果:
请输入您的姓名:王总
用户姓王
Process finished with exit code 0
这个答案,其实有些取巧,利用python提供的类型转行,将用户输入的数据转换成浮点数类型,如果转换抛异常,则判断数字不是纯数字组成
def test(num):
try:
return float(num)
except ValueError:
return "请输入数字"
print(test('133w3'))
a = 'This is string example….wow!'
b = 'Welcome To My World'
print(a.upper())
print(b.lower())
python提供了strip()方法,可以去除首尾空格
rstrip()去掉尾部空格
lstrip()去掉首部空格
replace(" ", “”) 去掉全部空格
a = ' welcome to my world '
print(a.strip())
还可以通过递归的方式实现
def trim(s):
flag = 0
if s[:1]==' ':
s = s[1:]
flag = 1
if s[-1:] == ' ':
s = s[:-1]
flag = 1
if flag==1:
return trim(s)
else:
return s
print(trim(' Hello world! '))
通过while循环实现
def trim(s):
while(True):
flag = 0
if s[:1]==' ':
s = s[1:]
flag = 1
if s[-1:] == ' ':
s = s[:-1]
flag = 1
if flag==0:
break
return s
print(trim(' Hello world! '))
def test():
s = 'ajldjlajfdljfddd'
# 定义一个数组存放数据
str_list = []
# for循环s字符串中的数据,然后将数据加入数组中
for i in s:
# 判断如果数组中已经存在这个字符串,则将字符串移除,加入新的字符串
if i in str_list:
str_list.remove(i)
str_list.append(i)
# 使用 sorted 方法,对字母进行排序
a = sorted(str_list)
# sorted方法返回的是一个列表,这边将列表数据转换成字符串
return "".join(a)
print(test())
结果:
adfjl
Process finished with exit code 0
def test():
n = 8
for i in range(-int(n/2), int(n/2) + 1):
print(" "*abs(i), "*"*abs(n-abs(i)*2))
print(test())
结果:
**
****
******
********
******
****
**
Process finished with exit code 0
class Test:
# 计算数字的位数
def test_num(self, num):
try:
# 定义一个 length 的变量,来计算数字的长度
length = 0
while num != 0:
# 判断当 num 不为 0 的时候,则每次都除以10取整
length += 1
num = int(num) // 10
if length > 5:
return "请输入正确的数字"
return length
except ValueError:
return "请输入正确的数字"
# 逆序打印出个位数
def test_sorted(self, num):
if self.test_num(num) != "请输入正确的数字":
# 逆序打印出数字
sorted_num = num[::-1]
# 返回逆序的个位数
return sorted_num[-1]
print(Test().test_sorted('12346'))
结果:
1
Process finished with exit code 0
如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。
例如:153 = 1^3 + 5^3 + 3^3,因此 153 就是一个水仙花数
那么问题来了,求1000以内的水仙花数(3位数)
def test():
for num in range(100, 1000):
i = num // 100
j = num // 10 % 10
k = num % 10
if i ** 3 + j ** 3 + k ** 3 == num:
print(str(num) + "是水仙花数")
test()
i = 1
for j in range(101):
i = j + i
print(i)
结果:
5051
Process finished with exit code 0
def test(sum_to):
# 定义一个初始值
sum_all = 0
# 循环想要计算的数据
for i in range(1, sum_to + 1):
sum_all += i * (-1) ** (1 + i)
return sum_all
if __name__ == '__main__':
result = test(sum_to=100)
print(result)
-50
Process finished with exit code 0
计算公式 13 + 23 + 33 + 43 + …….+ n3
实现要求:
输入 : n = 5
输出 : 225
对应的公式 : 13 + 23 + 33 + 43 + 53 = 225
def test(n):
sum = 0
for i in range(1, n+1):
sum += i*i*i
return sum
print(test(5))
结果:
225
Process finished with exit code 0
a = 'hello'
b = 'world'
c = a
a = b
b = c
print(a, b)
def test():
x = [1, 'a', 0, '2', 0, 'a', 1]
# 通过下标的形式,将字符串逆序进行比对
if x == x[::-1]:
return True
return False
print(test())
结果:
True
Process finished with exit code 0
def test():
a = [1, 3, 5, 7, 11]
# 逆序打印数组中的数据
print(a[::-1])
# 定义一个计数的变量
count = 0
for i in a:
# 判断每循环列表中的一个数据,则计数器中会 +1
count += 1
# 如果计数器为奇数,则打印出来
if count % 2 != 0:
print(i)
test()
结果:
[11, 7, 5, 3, 1]
1
5
11
Process finished with exit code 0
a = [1, 6, 8, 11, 9, 1, 8, 6, 8, 7, 8]
print(sorted(a))
结果:
[1, 1, 6, 6, 7, 8, 8, 8, 8, 9, 11]
Process finished with exit code 0
L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88]
print(max(L1))
print(min(L1))
结果:
88
1
Process finished with exit code 0
上面是通过python自带的函数,下面有可以自己写一个计算程序,贴代码:
class Test(object):
def __init__(self):
# 测试的列表数据
self.L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88]
# 从列表中取第一个值,对于数据大小比对
self.num = self.L1[0]
def test_small_num(self, count):
"""
:param count: count为 1,则表示计算最大值,为 2 时,表示最小值
:return:
"""
# for 循环查询列表中的数据
for i in self.L1:
if count == 1:
# 循环判断当数组中的数据比初始值小,则将初始值替换
if i > self.num:
self.num = i
elif count == 2:
if i < self.num:
self.num = i
elif count != 1 or count != 2:
return "请输入正确的数据"
return self.num
print(Test().test_small_num(1))
print(Test().test_small_num(2))
结果:
88
1
Process finished with exit code 0
def test():
a = ["hello", "world", "yoyo", "congratulations"]
# 统计数组中第一个值的长度
length = len(a[0])
for i in a:
# 循环数组中的数据,当数组中的数据比初始值length中的值长,则替换掉length的默认值
if len(i) > length:
length = i
return length
print(test())
结果:
congratulations
Process finished with exit code 0
def test():
L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88]
return sorted(L1)[:3]
print(test())
结果:
[1, 2, 2]
Process finished with exit code 0
def test():
a = [1, -6, 2, -5, 9, 4, 20, -3]
# 定义一个数组,存放处理后的绝对值数据
lists = []
for i in a:
# 使用 abs() 方法处理绝对值
lists.append(abs(i))
return lists
print(test())
结果:
[1, 6, 2, 5, 9, 4, 20, 3]
Process finished with exit code 0
def test():
b = ["hello", "helloworld", "he", "hao", "good"]
count = {
}
# 循环查看数组汇总每个字符串的长度
for i in b:
# 将数据统计称字典格式,字符串作为键,字符串长度作为值
count[i] = len(i)
# 按照字典的值,将字典数据从大到小排序
message = sorted(count.items(), key=lambda x:x[1], reverse=True)
lists = []
for j in message:
# 循环把处理后的数据,加入到新的数组中
lists.append(j[0])
print(lists)
test()
结果:
['helloworld', 'hello', 'good', 'hao', 'he']
Process finished with exit code 0
L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88]
如何用一行代码得出[1, 2, 3, 5, 11, 33, 88]
print(sorted(set(L1)))
结果:
[1, 2, 3, 5, 11, 33, 88]
Process finished with exit code 0
a = [3, 2, 1, 4, 2, 6, 1]
lists = []
for i in a:
if i not in lists:
lists.append(i)
print(lists)
结果:
[3, 2, 1, 4, 6]
Process finished with exit code 0
a = [1, 3, 5, 7]
b = ['a', 'b', 'c', 'd']
for i in b:
a.append(i)
print(a)
结果:
[1, 3, 5, 7, 'a', 'b', 'c', 'd']
Process finished with exit code 0
print([i for i in range(2, 11, 2) if i % 2 == 0])
结果:
[2, 4, 6, 8, 10]
Process finished with exit code 0
a = [1, 2, 3, 4, 5]
lists = []
for i in a:
lists.append(i*i)
print(lists)
结果:
[1, 4, 9, 16, 25]
Process finished with exit code 0
a = [1, 3, -3, 4, -2, 8, -7, 6]
print([i for i in a if i > 0])
结果:
[1, 3, 4, 8, 6]
Process finished with exit code 0
def test():
lists = [1, 3, 5, 7, 0, -1, -9, -4, -5, 8]
# 定义一个变量,计算正数
positive_num = 0
# 计算负数
negative_num = 0
for i in lists:
# 判断循环数组中的数据大于0,则正数会+1
if i > 0:
negative_num += 1
# 因为 0 既不是正数也不是负数,所以我们判断小于0为负数
elif i < 0:
positive_num += 1
return positive_num, negative_num
print(test())
结果:
(4, 5)
Process finished with exit code 0
def test():
a = ["张三", "张四", "张五", "王二"]
for i in a[:]:
if i[0] == '张':
a.remove(i)
return a
print(test())
结果:
['王二']
Process finished with exit code 0
在实现这个需求的时候,踩到了一个坑,就是当我在for循环判断数组中的姓名第一个等于张的时候,当时的代码判断是这样写的
for i in a:
if i[0] == '张':
然后打印出来的数据是 [‘张四’, ‘王二’],我当时还有写疑惑,我的逻辑判断是对的,为什么‘张四’这个名称会被打印出来,于是我打了一个断点查看了一下。
发现当第一个‘张三’被删除之后,再次循环时,直接跳过了‘张三’,百度查了才知道,如图:
感兴趣的小伙伴,可以查看这篇文章:https://www.cnblogs.com/zhouziyuan/p/10137086.html
a = [1, 3, 5, 7, 0, -1, -9, -4, -5, 8]
def test(a):
return a < 0
temlists = filter(test, a)
print(list(temlists))
结果:
[-1, -9, -4, -5]
Process finished with exit code 0
b = ["张三", "张四", "张五", "王二"]
def test(b):
return b[0] != '张'
print(list(filter(test, b)))
结果:
['王二']
Process finished with exit code 0
a = [
{
"name": "张三", "score": 66},
{
"name": "李四", "score": 88},
{
"name": "王五", "score": 90},
{
"name": "陈六", "score": 56}
]
print(list(filter(lambda x: x.get("score") >= 60, a)))
返回:
[{
'name': '张三', 'score': 66}, {
'name': '李四', 'score': 88}, {
'name': '王五', 'score': 90}]
def test():
a = [1, 2, 3, 11, 2, 5, 88, 3, 2, 5, 33]
# 找到数组中最大的数字
b = max(a)
count = 0
# 定义一个计数器,每次循环一个数字的时候,则计数器+1,用于记录数字的下标
for i in a:
count += 1
# 判断当循环到最大的数字时,则退出
if i == b:
break
return count -1
print(test())
结果:
6
Process finished with exit code 0
def test():
a = [
"my", "skills", "are", "poor", "I", "am", "poor", "I",
"need", "skills", "more", "my", "ability", "are",
"so", "poor"
]
dicts = {
}
for i in a:
# 统计数组中每个字符串出现的次数,将数据存入到字典中
if i not in dicts.keys():
dicts[i] = a.count(i)
# 找到字典中最大的key
return sorted(dicts.items(), key=lambda x: x[1], reverse=True)[0][0]
print(test())
结果:
poor
Process finished with exit code 0
def test(lists, string):
"""
:param lists: 数组
:param string: 查找的字符串
:return:
"""
# 判断字符串不再数组中,返回-1
if string not in lists:
return -1
count = 0
# 获取字符串当前所在的位置
for i in lists:
count += 1
if i == string:
return count - 1
print(test([1, "aa", "val", 2, "bb", "val", 33], 'val'))
结果:
2
Process finished with exit code 0
给定一个整数数组nums 和一个目标值target ,请你在该数组中找出和为目标值的那两个整数,并返回他
们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定nums=[2,7,11,15],target=9
因为nums[0] + nums[1] =2+7 = 9
所以返回[0, 1]
a = [[1,2],[3,4],[5,6]] 如何一句代码得到 [1, 2, 3, 4, 5, 6]
a = [[1, 2], [3, 4], [5, 6]]
# 定义一个新数组存放数据
lists = []
for i in a:
# 二次 for 循环,将数据存入到 lists 中
for j in i:
lists.append(j)
print(lists)
结果:
[1, 2, 3, 4, 5, 6]
Process finished with exit code 0
import numpy
a = [["A", 1], ["B", 2]]
x = numpy.array(a)
print(x[1, 1])
结果:
2
Process finished with exit code 0
L = [1, 2, 3, 5, 6]
# 使用推导式,将数组中的数字转成 str 类型
lists = [str(i) for i in L]
print(''.join(lists))
结果:
12356
Process finished with exit code 0
a = ["a", "b", "c"]
b = [1, 2, 3]
c = {
k: v for k, v in zip(a, b)}
print(c)
结果:
{
'a': 1, 'b': 2, 'c': 3}
people = [
{
"name": "yoyo", "age": 20},
{
"name": "admin", "age": 28},
{
"name": "zhangsan", "age": 25},
]
print(sorted(people, key=lambda x: x['age'], reverse=True))
结果:
[{
'name': 'admin', 'age': 28}, {
'name': 'zhangsan', 'age': 25}, {
'name': 'yoyo', 'age': 20}]
Process finished with exit code 0
nums=[2, 5, 7]
nums.append(9)
nums.insert(1, 0)
print(nums)
结果:
[2, 0, 5, 7, 9]
Process finished with exit code 0
import random
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
random.shuffle(a)
print(a)
结果:
[2, 7, 9, 4, 8, 1, 3, 5, 6]
Process finished with exit code 0