计算字符串前缀
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
# 前缀
x = input()
# 字符串的个数
n = int(input())
answer_count = 0
# 拿到每个字符串
for i in range(n):
y = input()
# 记录前缀为x的字符串的个数
answer_count += y.startswith(x)
print(answer_count)
今天看一个前缀字符串的函数startswith()
以上面这个题为例子,startswith(x)
用于检查字符串是否以指定的前缀 x
开头。如果字符串以 x
开头,则返回 True
,否则返回 False
。
startswith()函数
1.用法
str.startswith(prefix[, start[, end]])
2.参数
prefix
:要检查的前缀,可以是一个字符串或一个包含多个字符串的元组。start
(可选):字符串中开始检查的位置,默认为 0。end
(可选):字符串中结束检查的位置,默认为字符串的长度。
3.返回值
- 如果字符串以指定的前缀开头,返回
True
;否则返回False
。
4.举个例子
基本用法
text = "Hello, world!"
print(text.startswith("Hello")) # 输出: True
print(text.startswith("world")) # 输出: False
使用起始和结束位置
text = "Hello, world!"
print(text.startswith("world", 7)) # 输出: True
print(text.startswith("world", 0, 5)) # 输出: False(注意是每个字符一位)
检查多个前缀(注意这个是任意一个成立就true)
text = "Hello, world!"
print(text.startswith(("Hello", "Hi"))) # 输出: True
print(text.startswith(("Hi", "Hey"))) # 输出: False(两个都不满足所以false)
5.注意事项
startswith()
方法对大小写敏感。- 如果
prefix
是一个空字符串,startswith()
总是返回True
。(所以提前得检查)
6.实际应用
startswith()
方法常用于文件名过滤、数据验证和字符串解析等场景。例如:
filenames = ["data1.csv", "data2.csv", "image1.png", "image2.png"]
csv_files = [f for f in filenames if f.startswith("data")]
print(csv_files) # 输出: ['data1.csv', 'data2.csv']
可以有效地帮助过滤和处理大量字符串数据
或者手动算(作者:Stephen10121)
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
x = input()
f=0
n = int(input())
for i in range(n):
y = input()
f+=1 if y[0:len(x)]==x else 0
print(f)
这里直接获取前面的 l e n ( x ) len(x) len(x)个字符,也是一种方法
获取最大公约数
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
a, b = [int(i) for i in input().split()]
# 输出两个数的最大公约数
print(math.gcd(a, b))
输出文件路径
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
folder_list = []
num = int(input())
for i in range(num):
folder = input()
# 每个folder都用/隔开
folder_list.append(folder)
filename = input()
extension = input()
# 输出文件地址
print('/'.join(folder_list) + '/' + filename + '.' + extension)
游戏对抗系统的设定
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
power = input()
side = input()
# Your opponent's | Your
# power | attack direction | response | response direction
# -------+------------------+----------+--------------------
# usual | left/right/above | defence | left/right/above
# | | | (same as your
# | | | opponent)
# -------+------------------+----------+--------------------
# hard | left | dodging | right
# |------------------| |--------------------
# | right | | left
# |------------------| |--------------------
# | above | | any
response = str()
response_direction = str()
# 创建一个字典,用于存储不同情况下的响应和响应方向
response_dict = {'usual':{'left': 'defence', 'right': 'defence', 'above': 'defence'},
'hard': {'left': 'dodging', 'right': 'dodging', 'above': 'dodging'}}
response_direction_dict = {'usual': {'left': 'left', 'right': 'right', 'above': 'above'},
'hard': {'left': 'right', 'right': 'left', 'above': 'any'}}
# 根据输入的power和side,从字典中获取相应的响应和响应方向
response = response_dict[power][side]
response_direction = response_direction_dict[power][side]
print(response)
print(response_direction)