Python 增加数值字符串K个单位

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()函数逐一递增输入列表中的数字字符串,返回一个列表 –

# 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()函数来判断一个字符是否为数字。为了确定给定的字符是否是数字,我们还学会了如何使用可选的可迭代对象进行检查。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程