PAT (Basic Level) Practice Python解法 1039 到底买不买(replace用法)
import sys
sale = sys.stdin.readline()
goal = sys.stdin.readline()
cnt = 0
sale1 = sale
goal1 = goal
for i in goal:
if i in sale1:
cnt+=1
sale1 = sale1.replace(i, '',1)#replace会替换多次
goal1 = goal1.replace(i, '',1)
#print(goal1,sale1)
if cnt == len(goal):
print("Yes {:}".format(len(sale)-len(goal)))
else:
print('No {:}'.format(len(goal1)))#没找到对象
本文探讨了如何使用Python的replace()函数解决实际问题,如决定是否购买商品。通过读取输入的销售和目标清单,计算目标中未在销售中出现的项目的数量,实现'Yes'或'No'的输出。
&spm=1001.2101.3001.5002&articleId=120086875&d=1&t=3&u=b4abbb36f29847af9f7b3ea12dbd079a)

被折叠的 条评论
为什么被折叠?



