因为做题要用到,但我不太习惯这个,所以放这里供以后自己做题参考。够做题就行.jpg
单行输入值读取
1. 单行输入一个值
b = map(int, input().strip())
input(‘注释文字’):自己向系统输入。input输出str类型,单一值,可把input的输出结果用str相关函数来处理。
map(函数,一个或多个序列), 函数对序列中每个元素进行操作; python3.x返回迭代器。
#Python3.x 实例
>>> def square(x) : # 计算平方数
... return x ** 2
...
>>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方
<map object at 0x100d3d550> # 返回迭代器
>>> list(map(square, [1,2,3,4,5])) # 使用 list() 转换为列表
[1, 4, 9, 16, 25]
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5])) # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
str.strip(‘char’):去除str头尾的char,char包含的字符只要在头尾就全部去掉。
a = input().strip('12')
>输入12321
>>>3
>输入1232
>>>3
>输入2131
>>>3
>输入22311
>>>3
2 单行输入多个值
a, b = map(int, input().split())
str.split(‘char’,num):char表示分割标志,默认空字符(空格、\n、\t等),num表示分割次数,默认为char个数;输出字符串列表
str = "this is string example....wow!!!"
print (str.split( )) # 以空格为分隔符
print (str.split('i',1)) # 以 i 为分隔符
>>>['this', 'is', 'string', 'example....wow!!!']
>>>['th', 's is string example....wow!!!']
多行输入值读取
1. 确定行数
用for循环
输入如下:
3
1 2 3
5
n = int(input())
for i in range(n):
tmp = map(int,input().strip())
print(list(tmp))
2 不确定行数
while True:
try:
progress
except:
break
这篇博客介绍了ACM竞赛中如何处理输入输出,包括单行输入一个值和多个值的方法,以及如何处理多行输入值,特别是确定和不确定行数的情况。文中详细解释了`input()`、`map()`和`split()`函数的使用。
1463

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



