Python输入多行数据

it2023-08-06  75

Python 输入多行数据

split() 默认以空格形式分隔字符split(’,’) 以逗号形式分隔字符map(function,iterable,…),这里使用function=int或者float修改输入数据的类型可以在map函数外再嵌套一个list(),将数据存入一个列表 # 一行输入两个字符串,用空格隔开 m, n = input().split() print(m,n) >>>请输入字符串apple pi >>>apple pi # 一行输入两个字符串,用逗号隔开 m,n=input().split(',') print(m,n) >>>apple,pi >>>apple pi # 一行输入两个数字,用空格隔开;再用map()函数处理数据 m, n = map(int, input("请输入两个数字").split()) a,b=map(float,input("请输入两个数字").split()) print(m,n) print(a,b) >>>请输入两个数字21 22 >>>请输入两个数字21.1 23 >>>21 22 >>>21.1 23.0 # 一行输入多个数字,空格隔开,存入列表a中 a = list(map(int, input().split())) print(a) >>>21 22 34 54 67 >>>[21, 22, 34, 54, 67] # 多行输入:先输入一个数字代表需要输入几行,比如Q=4,那么就需要再输入4行数据 Q = int(input()) q = [] for i in range(Q): q.append(list(map(int, input().rstrip().split()))) #rstrip()删除字符串尾部的空字符 print(q) >>>4 >>>11 22 33 44 55 >>>21 23 34 45 56 67 88 >>>22 33 44 55 66 >>>90 89 67 45 67 78 >>>[[11, 22, 33, 44, 55], [21, 23, 34, 45, 56, 67, 88], [22, 33, 44, 55, 66], [90, 89, 67, 45, 67, 78]]
最新回复(0)