這對初學者來說是一個常見的問題。面試的時候可能會問。有時,開發人員還需要在一行中接受多個輸入。可以使用 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
有一個流行的庫叫做 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]]
在本教程中,我們展示了從用戶處獲取多個值的不同方法。它節省了代碼行數,並且非常容易使用。我們還將它描述為一個矩陣,我們可以在其中創建一個用戶定義的矩陣。
原創文章,作者:0INYO,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126871.html