简体   繁体   English

如何在python 2的同一行上获取不同数据类型的多个输入? 不能使用 map() 函数,因为输入有混合数据

[英]How to take multiple inputs of different data types on same line in python 2? can't use map() function as input has mixed data

For ex: i want to take input as A 50 60 70. A is student name and next three numbers are marks of A in 3 subjects.例如:我想输入 A 50 60 70。A 是学生姓名,接下来的三个数字是 3 个科目的 A 分数。 Help me in storing data as above.帮助我存储上述数据。 I have to collect that data for n students.我必须为 n 个学生收集这些数据。

n=input()
for i in range(0,n):
    St=raw_input(), Eng,Mat,Sci=map(int,raw_input().split())
    print st
    print Eng,Mat,Sci

Error:错误:

sh-4.3$ python main.py                                                                                                                                                                                        
  File "main.py", line 3                                                                                                                                                                                      
    St=raw_input(), Eng,Mat,Sci=map(int,raw_input().split())                                                                                                                                                  
SyntaxError: can't assign to function call 

You can keep multiple lines of code in one line by separating them with a ;您可以将多行代码保留在一行中,方法是用;分隔它们; , as long as they're in the same indent level. ,只要它们处于相同的缩进级别。 And since it's python 2, shouldn't n=int(raw_input()) ?而且因为它是 python 2,不应该n=int(raw_input())吗?

n=int(raw_input())
for i in range(0,n):
    St=raw_input(); Eng,Mat,Sci=map(int,raw_input().split())
    print st
    print Eng,Mat,Sci

But...this isn't a good practice, as it makes your code harder to read.但是……这不是一个好习惯,因为它会使您的代码更难阅读。 You should separate them on different lines.您应该将它们分开在不同的行上。

n=int(raw_input())
for i in range(0,n):
    St=raw_input()
    Eng,Mat,Sci=map(int,raw_input().split())
    print st
    print Eng,Mat,Sci

In Python, split() function works for taking multiple inputs as a list data type.在 Python 中, split() 函数用于将多个输入作为列表数据类型。 If you need int,char or string type data then you need to apply typecasting of Python.In the solution,I am using the same approach.如果您需要 int、char 或 string 类型的数据,那么您需要应用 Python 的类型转换。在解决方案中,我使用相同的方法。

n=int(raw_input())
for i in range(0,n):
     s=str(raw_input())
     St,Eng,Mat,Sci=raw_input().split()
     Eng=int(Eng)
     Mat=int(Mat)
     Sci=int(Sci)
     print St,s
     print Eng,Mat,Sci

I think this would do the job in python 3.我认为这可以在 python 3 中完成这项工作。

n = int(input())
marks = {}
for i in range(n):
    name, *line = input().split()
    scores = list(map(float, line))
    marks[name] = scores

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中使用不同规则从同一行获取多个输入 - how to take multiple inputs in python from same line with different rules 如何在python3中从单行输入的单行代码中映射不同数据类型的值? - How to map values of different data types in a single line of code from a single line of input in python3? 列表中的python输入混合数据类型 - python input mixed data types in list Python:如何使 map() 跳过无法转换的数据类型? - Python: How to make map() skip data types that can't be transformed? 如何读取 python 中文件同一行上列出的多个输入数据 - how to read multiple input data listed on the same line of a file in python Python_ 在一行中获取不同数据类型的多个输入 - Python_ Taking multiple inputs of different data type in one line 需要更改大写python以使用raw_input函数解释3种不同类型的输入SAME - Need to change capitalize python to interpret 3 different types of inputs the SAME with the raw_input function 如何从文本中提取一行用作python函数的输入 - how to take a line from a text to use as input for a function python 将 Python Keras 机器学习模型转换为可重复的函数,可以将多个 X 和 y 数据集作为输入 - Turning Python Keras Machine Learning model into repeatable function that can take as input multiple X and y data sets 如何有效地使用 input().split() 在一行中从用户那里获取至少 2 个输入? - How can i use input().split() effectively to take at least 2 inputs from user in one line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM