如何在 Python 中獲取用戶的多重輸入

這對初學者來說是一個常見的問題。面試的時候可能會問。有時,開發人員還需要在一行中接受多個輸入。可以使用 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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
0INYO的頭像0INYO
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相關推薦

  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • 如何在PyCharm中安裝OpenCV?

    本文將從以下幾個方面詳細介紹如何在PyCharm中安裝OpenCV。 一、安裝Python 在安裝OpenCV之前,請確保已經安裝了Python。 如果您還沒有安裝Python,可…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29

發表回復

登錄後才能評論