Python程式:將數字字串遞增K
在Python中,數字字串是指使用數字、符號和小數點表示數值的字串,允許進行數學運算和資料處理。
在這篇文章中,我們將學習一個Python程式,用於將數字字串遞增K。
示例
假設我們已經得到一個**輸入字串列表**。我們將使用上述方法將輸入列表中的數字字串遞增k。
輸入
inputList = ["10", "hello", "8", "tutorialspoint", "12", "20"] k = 5
輸出
['15', 'hello', '13', 'tutorialspoint', '17', '25']
在上例列表中,10、8、12、20是數字字串。因此,輸入的K值(即5)將新增到所有這些數字字串中,並列印結果字串。
10+5 = 15
8+5 = 13
12+5= 17
20+5 = 25
因此,結果列表為['15', 'hello', '13', 'tutorialspoint', '17', '25']
演算法(步驟)
以下是執行所需任務的演算法/步驟
建立一個變數來儲存**輸入字串列表**並列印列表。
建立一個另一個變數來儲存**輸入k**值。
初始化一個空列表用於儲存結果列表。
使用for迴圈遍歷輸入列表的每個元素。
使用**if條件**語句和**isdigit()**函式檢查當前元素是否為數字(如果所有字元都是數字,則isdigit()方法返回True;否則,返回False)。
使用**int()**函式將元素轉換為整數,然後加上k,再使用**str()**函式轉換為字串,最後使用**append()**函式將其新增到結果列表的末尾(在列表末尾新增元素)。
否則,直接將當前元素新增到結果列表。
將輸入列表中的數字字串遞增K後,列印結果列表。
示例1:使用for迴圈和isdigit()函式
以下程式使用for迴圈和isdigit()函式返回一個列表,該列表在輸入列表中將數字字串遞增K。
# input list of strings
inputList = ["10", "hello", "8", "tutorialspoint", "12", "20"]
# printing input list
print("Input List: ", inputList)
# input k
k = 5
# resultant list
resultantList = []
# traversing through each element of the input list
for e in inputList:
# checking whether the current element is a digit or not
if e.isdigit():
# converting element to integer and adding k to it then converting
# to string for appending it to the resultant list
resultantList.append(str(int(e) + k))
else:
# else appending that element to the resultant list directly
resultantList.append(e)
# printing resultant list
print("Incrementing numeric strings in input list by 5:\n", resultantList)
輸出
執行上述程式將生成以下輸出
Input List: ['10', 'hello', '8', 'tutorialspoint', '12', '20'] Incrementing numeric strings in input list by 5: ['15', 'hello', '13', 'tutorialspoint', '17', '25']
示例2:使用列表推導式和isdigit()函式
當您希望基於現有列表的值構建新列表時,列表推導式提供了一種更短/簡潔的語法。
以下程式使用列表推導式和isdigit()函式返回一個列表,該列表在輸入列表中將數字字串遞增K。
# input list of strings
inputList = ["10", "hello", "8", "tutorialspoint", "12", "20"]
# printing input list
print("Input List: ", inputList)
# input k
k = 5
# Using list comprehension for concise syntax
resultantList = [str(int(e) + k) if e.isdigit() else e for e in inputList]
# printing resultant list
print("Incrementing numeric strings in input list by 5:\n", resultantList)
輸出
執行上述程式將生成以下輸出:
Input List: ['10', 'hello', '8', 'tutorialspoint', '12', '20'] Incrementing numeric strings in input list by 5: ['15', 'hello', '13', 'tutorialspoint', '17', '25']
方法3:不使用任何內建函式
以下程式返回一個列表,在輸入列表中將數字字串遞增K,而不使用任何內建函式。
# creating a function that accepts the list element as an argument
# and checks whether it is a numeric number or not
def checkNumeric(n):
# initializing all the digits as a string
digitsStr = "0123456789"
# storing the count of the number of digits in the given string
count = 0
# traversing through each character of the list element
for c in n:
# checking whether the current character is in the digits string
if c in digitsStr:
# incrementing the count value by 1
count += 1
# checking whether the count is equal to the length of the list element
if(count == len(n)):
# returning true if the condition is true
return True
# returning False
return False
# input list of strings
inputList = ["10", "hello", "8", "tutorialspoint", "12", "20"]
# printing input list
print("Input List: ", inputList)
# input k
k = 5
# storing resultant list after incrementing numeric strings
resultantList = []
# traversing through each element of an input list
for e in inputList:
# checking whether the current element is numeric or not by
# calling checkNumeric() function
if(checkNumeric(e)):
# converting element to integer and adding k to it then converting
# to string for appending it to the resultant list
resultantList.append(str(int(e)+k))
else:
# else appending that element to the resultant list directly
resultantList.append(e)
# printing resultant list
print("Incrementing numeric strings in input list by 5:\n", resultantList)
輸出
執行上述程式將生成以下輸出
Input List: ['10', 'hello', '8', 'tutorialspoint', '12', '20'] Incrementing numeric strings in input list by 5: ['15', 'hello', '13', 'tutorialspoint', '17', '25']
結論
在本文中,我們學習了三種不同的方法來將數字字串遞增K。我們學習瞭如何使用isdigit()函式來確定字元是否為數字。為了確定給定字元是否為數字,我們還學習瞭如何獲取可選的迭代器並進行檢查。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP