python输入多组数据_Python3算法类多组数据输入输出格式

在 Python3 中舍弃了 Python2 中的 raw_input() 的输入方式,读入的数据全部是字符串类型,需要使用 int() 强转即可,

input()读入一行数据,strip() 去除两端空格, split() 默认按照空格分割

map方式的原理:

map(function, iterable, ...)

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed,function must

take that many arguments and is applied to the items from all iterables

in parallel. If one iterable is shorter than another it is assumed to

be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

map的第一个参数可以是 function.

第一种(有多组输入数据,但是没有告诉你有多少组):# 1)、

while True:

try:

a, b = input().strip().split()

print(int(a) + int(b))

except :

break

# 2)、

# 是另外一种读入方式,下面都会按照这种方式

while True:

try:

a, b = map(int, input().strip().split())

print(a+ b)

except :

break

第二种(输入一个整数,告诉我们接下来有多少组数据)t = int(input().strip())

for i in range(t):

a, b = map(int, input().strip().split())

print(a+ b)

第三种(有多组输入数据,没有告诉你有多少组,但是题目告诉你遇见什么结束)while True:

a, b = map(int, input().strip().split())

if a == 0 and b == 0:

break

print(a+ b)

第四种(有多组输入数据,并告诉我们有多少组和遇见什么结束)t = int(input().strip())

for i in range(t):

a, b = map(int, input().strip().split())

if a == 0 and b == 0:

break

print(a+ b)

有什么问题,请在下面进行留言

待续....

  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值