如何在 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/n/126871.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
0INYO0INYO
上一篇 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

发表回复

登录后才能评论