首页 > 编程知识 正文

如何在 Python 中获取用户的多重输入

时间:2023-11-27 14:11:03 阅读:309465 作者:IQRG

这对初学者来说是一个常见的问题。面试的时候可能会问。有时,开发人员还需要在一行中接受多个输入。可以使用 scanf() 方法在 C/C++中轻松完成。然而,Python 提供了两种方法来帮助我们在一行中获取多个值或输入。

  • 使用拆分()方法
  • 使用列表推导

在本教程中,我们将学习如何使用各种方法在一行中获取多个输入。

使用拆分()方法

split() 方法对于获取用户的多个输入很有用。语法如下。

语法-


input().split(separator, maxsplit)

参数-

  • 分隔符参数用指定的分隔符分隔输入。默认情况下,空白是指定的分隔符。

split() 方法用于拆分 Python 字符串,但是我们可以用它来获取多个值。

让我们理解下面的例子。

示例-


# taking two inputs at a time
a, b, c = input("Enter three values: ").split()
print("Enter Your First Name: ", a)
print("Enter Your Last Name: ", b)
print("Enter Your Class: ", c)
print()

# taking three inputs at a time
x, y, z = input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of passed student : ", y)
print("Number of failed student : ", z)
print()

# taking four inputs at a time
a, b, c, d = input("Enter four values: ").split()
print("First number is {}, second number is {} third is {} and fourth is {}".format(a, b, c, d))
print()

输出:

Enter three values: David Warner MCA
Enter Your First Name:  David
Enter Your Last Name:  Warner
Enter Your Class:  Warner

Enter three values: 100 67 33
Total number of students:  100
Number of passed students :  67
Number of failed students :  33

Enter four values: 1 2 3 4
First number is 1, second number is 2 third is 3 and fourth is 4
Enter multiple values: 4 6 7 2 4
List of students:  [4, 6, 7, 2, 4]

解释-

在上面的代码中,我们将多个输入放在一行中。这些值由空格分隔,您可以使用逗号(,)或任何其他符号。

使用列表推导

我们还可以使用 map() 方法和 split() 方法将值转换成列表。让我们理解下面的例子。

示例-2:


# Taking multiple inputs in a single line
# and type casting using list() function
x = list(map(int, input("Enter multiple values: ").split()))
print("List of students: ", x)

输出:

Enter multiple values: 4 6 7 2 4
List of values:  [4, 6, 7, 2, 4]

解释-

我们在上面的代码中使用空白作为分隔符,将输入放在一行中,并将其输入到一个列表中。

从用户处获取矩阵输入

矩阵是一个矩形阵列,或者我们可以说是数据或数字的矩形排列。矩阵可以取任何值,如整数值、浮点值、字符串、复数等。水平放置的值称为行,垂直放置的值称为列。如果矩阵由 r 行数和 c 列数组成,则矩阵的顺序为 r x c.

示例-


row = int(input("Enter the number of rows:"))
column = int(input("Enter the number of columns:"))

# Initialize matrix
matrix = []
print("Enter the entries row-wise:")

# For user input
for i in range(row):         # A for loop for row entries
    a =[]
    for j in range(column):  # A for loop for column entries
        a.append(int(input()))
    matrix.append(a)

# For printing the matrix
for i in range(row):
    for j in range(column):
        print(matrix[i][j], end = " ")
    print()

输出:

Enter the number of rows:3
Enter the number of columns:3
Enter the entries row-wise:
1
2
3
4
5
6
7
8
9
1 2 3 
4 5 6
7 8 9

以上可以在下面的一行中实现。

示例-


row = int(input("Enter the number of rows:"))
column = int(input("Enter the number of columns:"))
matrix = [[int(input()) for x in range (column)] for y in range(row)]
print(matrix)

输出:

Enter the number of rows:3
Enter the number of columns:3
1
2
3
4
5
6
7
8
9
1 2 3 
4 5 6
7 8 9

使用 map()函数和 numpy 库

有一个流行的库叫做 Numpy,它可以用于任何科学计算。它为多维数组提供了广泛的支持。我们将使用这个库作为输入矩阵。让我们理解下面的例子。

示例-


import numpy as np

row = int(input("Enter the number of rows:"))
column = int(input("Enter the number of columns:"))

print("Enter the entries in a single line (separated by space): ")

# User input of entries in a 
# single line separated by space
entries = list(map(int, input().split()))

# For printing the matrix
matrix = np.array(entries).reshape(row, column)
print(matrix)

输出:

Enter the number of rows:2
Enter the number of columns:2
Enter the entries in a single line separated by space: 1 2 3 1 
[[1 2]
 [3 1]]

结论

在本教程中,我们展示了从用户处获取多个值的不同方法。它节省了代码行数,并且非常容易使用。我们还将它描述为一个矩阵,我们可以在其中创建一个用户定义的矩阵。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。