Python程序提取带数字的字符串

Python程序提取带数字的字符串

在本文中,我们将学习如何在Python中提取带数字的字符串。

使用的方法

以下是实现此任务的各种方法 –

  • 使用列表推导式、any() 和 isdigit() 函数

  • 使用 any()、filter() 和 lambda 函数

  • 不使用任何内置函数

  • 使用 ord() 函数

例子

假设我们已经输入一个包含字符串的列表,我们现在将使用上述方法提取包含数字的字符串列表。

输入

inputList = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']

输出

包含任何数字的字符串列表:
['hello562', 'python20', '100']

在上面的输入列表中,元素 ‘hello562’、’python20’ 和 ‘100’ 包含数字。因此,它们从输入列表中被提取出来。

方法1:使用列表推导式、any() 和 isdigit() 函数

算法(步骤)

以下是执行所需任务的算法/步骤 –

  • 创建一个变量来存储字符串的输入列表

  • 打印输入列表。

  • 使用列表推导式遍历输入列表元素,并使用 any() 函数(如果迭代器中有任何元素为真,则返回 True,否则返回 False)和 isdigit() 函数逐字符检查字符串是否包含任何数字字符

  • isdigit() 函数 – 如果所有字符都是数字,则 isdigit() 方法返回 True;否则返回 False。

  • 打印包含任何数字的字符串的结果列表。

例子

以下程序使用列表推导式、any() 和 isdigit() 函数返回一个包含在输入列表中提取数字字符串的列表 –

# 字符串输入列表
inputList = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']

# 打印输入列表
print("输入列表:", inputList)

# 遍历列表元素并检查字符串是否包含任何数字字符
outputList = [item for item in inputList if any(c for c in item if c.isdigit())]

# 打印包含任何数字的字符串列表的结果
print("包含任何数字的字符串列表:\n", outputList)

输出

执行上述程序,将生成以下输出 –

输入列表:['hello562', 'tutorialspoint', 'python20', 'codes', '100']
包含任何数字的字符串列表:
['hello562', 'python20', '100']

方法2:使用 any()、filter() 和 lambda 函数

filter() 函数 – 使用确定序列中的每个元素是真还是假的函数来过滤指定的序列。

Lambda 函数

lambda 函数是一个匿名函数,它很小。

lambda 函数可以有任意数量/数量的参数,但仅有一个表达式。

语法

lambda arguments : expression

例子

以下程序使用 any()、filter() 和 lambda 函数返回一个包含在输入列表中提取数字字符串的列表 –

# 输入字符串列表
inputList = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']

# 打印输入列表
print("输入列表:", inputList)

# 过滤掉列表中包含数字字符串的元素
outputList = list(filter(lambda s: any( e for e in s if e.isdigit()), inputList))

# 打印结果列表中包含任何数字的字符串
print("包含任何数字的字符串列表:\n", outputList)

输出

执行上述程序将生成以下输出-

输入列表:['hello562', 'tutorialspoint', 'python20', 'codes', '100']
包含任何数字的字符串列表:
['hello562', 'python20', '100']

方法3:不使用任何内置函数

replace() 函数 − 返回替换所有旧子字符串为另一个新子字符串的字符串副本。

语法

string.replace(old, new, count)

示例

以下程序返回从不使用任何内置函数提取包含数字的字符串的列表-

# 接受输入字符串作为参数并返回包含数字的字符串的函数
def newFunction(str):

   # 初始化所有数字
   digitsStr = "0123456789"

   # 将传递给函数的字符串存储在temp变量中
   temp = str

   # 遍历digits字符串中的每个数字
   for d in digitsStr:

      # 用空格替换该数字
      str = str.replace(d, "")

   # 检查tmp变量字符串的长度是否不等于传递的字符串
   if(len(temp) != len(str)):

      # 如果条件为真,则返回true
      return True

      # 否则返回false
      return False

# 输入字符串列表
inputList = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']

# 打印输入列表
print("输入列表:", inputList)

# 用于存储包含数字的列表元素的空列表
outputList = []

# 遍历输入列表的每个元素
for element in inputList:

   # 检查上面定义的newFunction()是否返回true
   if newFunction(element):

      # 将该元素附加到输出列表中
         outputList.append(element)

# 打印结果列表中包含任何数字的字符串
print("包含任何数字的字符串列表:\n", outputList)

输出

执行上述程序将生成以下输出-

输入列表:['hello562', 'tutorialspoint', 'python20', 'codes', '100']
包含任何数字的字符串列表:
['hello562', 'python20', '100']

方法4:使用ord()函数

ord() 函数 − 以数字形式返回给定字符的Unicode码。

示例

以下程序返回从不使用ord()函数提取包含数字的字符串的列表-

# 输入字符串列表
inputList = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']

# 打印输入列表
print("输入列表:", inputList)

# 用于存储包含数字的列表元素的空列表
outputList = []

# 遍历输入列表的每个元素
for element in inputList:

   # 遍历当前元素的每个字符
   for c in element:

      # 检查字符的ASCII值是否大于或等于0且小于或等于9
      if(ord(c) >= ord('0') and ord(c) <= ord('9')):

         # 将该元素附加到输出列表中
            outputList.append(element)

         # 破坏循环
            break

# 打印结果列表中包含任何数字的字符串
print("包含任何数字的字符串列表:\n", outputList)

输出

在执行上述程序时,将生成以下输出 –

输入列表:['hello562','tutorialspoint','python20','codes','100']
包含任何数字的字符串列表:
['hello562','python20','100']

结论

从给定的字符串列表中,我们学习了提取包含数字的字符串元素的四种不同方法。 我们还学习了如何根据条件过滤列表。 我们还学习到,可以使用lambda函数内的any()方法应用条件,而不是使用嵌套列表。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程