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。

更新於: 2023年7月31日

72次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告