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