Python程序从每个数字中减去K
在本文中,我们将学习一个Python程序,用于从列表中的每个数字中减去K。
假设我们已经获取了一个包含数字的**输入列表**和**K**值。我们现在将从列表元素的每个数字中减去**k**,并使用以下方法打印结果列表。
示例
Let inputList = [1034, 356, 2145, 6712, 8671] Given input k value = 3
现在我们考虑第一个元素,即1034
从1034的每个数字中减去3,即:
1-3 = -2。所以,它向上取整为0
0-3 = -3。所以,它向上取整为0
3-3 = 0。它没有值
4-3 = 1。因此保留该值
因此,**1034**的输出为0001,即**1**。类似地,获取其他列表元素的所有值。
所以,**输出列表为:[1, 23, 12, 3400, 5340]**
使用的方法
使用for循环、str()、-运算符
使用列表推导式、str()、-运算符
使用NumPy的'-'运算符。
方法1:使用for循环、str()、-运算符
**max()**方法(返回可迭代对象中值最大的项/最大的数字)。**str()函数**(返回对象的字符串格式,即将其转换为字符串)。
算法(步骤)
以下是执行所需任务的算法/步骤。
创建一个变量来存储输入列表并打印它。
创建另一个变量来存储要从列表元素的每个数字中减去的输入k值。
创建一个新的空列表,用于存储从每个数字中减去k后的元素的结果列表。
使用**for循环**遍历输入列表的元素。
使用str()函数将数字转换为字符串。
遍历字符串的数字。从每个数字中减去k,以找到(0, 结果)的最大值。
使用join()函数连接减法后的结果。
使用int()函数将结果转换为整数以移除**前导零**。
使用append()函数将结果添加到resultList中。
打印从每个数字中减去K后的结果列表。
示例
以下程序使用for循环、str()、-运算符返回从输入列表元素的数字中减去给定k后的列表。
# input list
inputList = [1034, 356, 2145, 6712, 8671]
# printing the input list
print("Input list:\n", inputList)
# input k value (to be subtracted)
k = 3
# resultant list for storing elements after subtraction of k from each digit
resultList = []
# traversing through the elements of input list
for item in inputList:
# Converting the number to a string
strItem = str(item)
# Traversing through the digits of the string
# Subtracting k from each digit and finding max of 0 and this result.
# Joining all the results using the join() function
reslt = ''.join([str(max(0, int(i) - k)) for i in strItem])
# Converting the result to an integer and adding it to the result list
resultList.append(int(reslt))
# printing resultant list after subtracting K from each digit
print("Resultant list after subtracting K from each digit:\n", resultList)
输出
执行上述程序将生成以下输出:
Input list: [1034, 356, 2145, 6712, 8671] Resultant list after subtracting K from each digit: [1, 23, 12, 3400, 5340]
方法2:使用列表推导式、str()、-运算符
列表推导式
当您希望基于现有列表的值构建一个新列表时,列表推导式提供了更短/更简洁的语法。
示例
以下程序使用列表推导式、str()、-运算符返回从输入列表元素的数字中减去给定k后的列表。
# input list
inputList = [1034, 356, 2145, 6712, 8671]
# printing the input list
print("Input list:\n", inputList)
# input k value (to be subtracted)
k = 3
# Finding result using the list comprehension(concise syntax)
resultList = [int(''.join([str(max(0, int(i) - k)) for i in str(i)])) for i in inputList]
# printing resultant list after subtracting K from each digit
print("Resultant list after subtracting K from each digit:\n", resultList)
输出
执行上述程序将生成以下输出:
Input list: [1034, 356, 2145, 6712, 8671] Resultant list after subtracting K from each digit: [1, 23, 12, 3400, 5340]
方法3:使用NumPy的'-'运算符
以下程序使用NumPy的'-'运算符返回从输入列表元素的数字中减去给定k后的列表。
import numpy
# input list
inputList = [1034, 356, 2145, 6712, 8671]
# printing the input list
print("Input list:\n", inputList)
# input k value (to be subtracted)
k = 3
# resultant list for storing elements after subtraction of k from each digit
resultList = []
# traversing through the elements of the input list
for item in inputList:
# Creating a new list to store the list of digits
listOfDigits = []
# Traversing through the digits of the list
for digit in str(item):
# Converting the digit to an integer and appending it to the list
listOfDigits.append(int(digit))
# Converting the above list of digits to Numpy Array
numpyArray = numpy.array(listOfDigits)
# Subracting K from each digit
numpyArray = numpyArray - k
# Taking an empty string to store the result
reslt = ""
# Traversing through the digits of the Numpy Array
for digit in numpyArray:
reslt += str(max(0, digit))
# Converting the result to an integer and adding it to the result list
resultList.append(int(reslt))
# printing resultant list after subtracting K from each digit
print("Resultant list after subtracting K from each digit:\n", resultList)
输出
执行上述程序将生成以下输出:
Input list: [1034, 356, 2145, 6712, 8671] Resultant list after subtracting K from each digit: [1, 23, 12, 3400, 5340]
结论
在本文中,我们学习了如何使用3种不同的方法从给定数字列表中的每个数字中减去k。正如我们所学到的,列表推导式提供了一种简洁的语法,使我们能够用更少的代码行完成工作。我们还学习了如何获取数字的字符串,将其转换为NumPy数组,并使用'-'运算符从NumPy数组的每个元素中减去k。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP