Python程序提取包含数字的字符串
在本文中,我们将学习如何在python中提取包含数字的字符串。
使用的方法
以下是完成此任务的各种方法:
使用列表推导式、any()和isdigit()函数
使用any()、filter()和lambda函数
不使用任何内置函数
使用ord()函数
示例
假设我们已经获取了一个包含字符串的列表作为输入。我们现在将使用上述方法从输入列表中提取包含数字的字符串。
输入
inputList = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']
输出
List of Strings containing any digits: ['hello562', 'python20', '100']
在上述输入列表中,元素'hello562'、'python20'、'100'包含数字。因此,它们从输入列表中提取出来。
方法1:使用列表推导式、any()和isdigit()函数
算法(步骤)
以下是执行所需任务的算法/步骤:
创建一个变量来存储输入字符串列表
打印输入列表。
使用列表推导式遍历输入列表元素,并使用any()函数(如果iterable中的任何项目为真,则返回True,否则返回False)和isdigit()函数逐字符检查字符串是否包含任何数字字符
isdigit()函数 - 如果所有字符都是数字,则isdigit()方法返回True;否则,返回False。
打印包含任何数字的字符串的结果列表。
示例
以下程序返回一个列表,该列表使用列表推导式、any()和isdigit()函数从输入列表中提取包含数字的字符串:
# input list of strings
inputList = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']
# printing input list
print("Input list:", inputList)
# traversing through list elements and checking if the string
# contains any digit character
outputList = [item for item in inputList if any(c for c in item if c.isdigit())]
# printing resultant list of Strings containing any digits
print("List of Strings containing any digits:\n", outputList)
输出
执行上述程序后,将生成以下输出:
Input list: ['hello562', 'tutorialspoint', 'python20', 'codes', '100'] List of Strings containing any digits: ['hello562', 'python20', '100']
方法2:使用any()、filter()和lambda函数
filter()函数 - 使用一个函数过滤指定的序列,该函数确定序列中的每个元素是为真还是为假。
Lambda函数
lambda函数是一个小的匿名函数。
lambda函数可以有无限/任意数量的参数,但只有一个表达式。
语法
lambda arguments : expression
示例
以下程序返回一个列表,该列表使用any()、filter()和lambda函数从输入列表中提取包含数字的字符串:
# input list of strings
inputList = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']
# printing input list
print("Input list:", inputList)
# filtering the list elements if any string character is a digit
outputList = list(filter(lambda s: any( e for e in s if e.isdigit()), inputList))
# printing resultant list of Strings containing any digits
print("List of Strings containing any digits:\n", outputList)
输出
执行上述程序后,将生成以下输出:
Input list: ['hello562', 'tutorialspoint', 'python20', 'codes', '100'] List of Strings containing any digits: ['hello562', 'python20', '100']
方法3:不使用任何内置函数
replace()函数 - 返回字符串的副本,该副本用另一个新子字符串替换旧子字符串的所有出现。
语法
string.replace(old, new, count)
示例
以下程序返回一个列表,该列表在不使用任何内置函数的情况下从输入列表中提取包含数字的字符串:
# function that returns string with digits by accepting input string as an argument
def newFunction(str):
# initializing all digits
digitsStr= "0123456789"
# storing the string passed to the function in a temp variable
temp = str
# traversing through each digit in the above digits string
for d in digitsStr:
# replacing that digit with space
str = str.replace(d, "")
# checking whether the length of the temp variable string is not equal to the passed string
if(len(temp) != len(str)):
# returning true if the condition is true
return True
# else returning false
return False
# input list of strings
inputList = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']
# printing input list
print("Input list:", inputList)
# empty list for storing list elements containing digits
outputList = []
# traversing through each element of the input list
for element in inputList:
# checking whether the above defined newFunction() returns true
if newFunction(element):
# appending that element to the output list
outputList.append(element)
# printing resultant list of Strings containing any digits
print("List of Strings containing any digits:\n", outputList)
输出
执行上述程序后,将生成以下输出:
Input list: ['hello562', 'tutorialspoint', 'python20', 'codes', '100'] List of Strings containing any digits: ['hello562', 'python20', '100']
方法4:使用ord()函数
ord()函数 - 将给定字符的Unicode代码作为数字返回。
示例
以下程序返回一个列表,该列表在不使用ord()函数的情况下从输入列表中提取包含数字的字符串:
# input list of strings
inputList = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']
# printing input list
print("Input list:", inputList)
# empty list for storing list elements containing digits
outputList = []
# traversing through each element of the input list
for element in inputList:
# traversing through each character of the current element
for c in element:
# checking whether the ASCII value of char is greater than or
# equal to 0 and less than or equal to 9
if(ord(c) >= ord('0') and ord(c) <= ord('9')):
# appending that element to output list
outputList.append(element)
# breaking the loop
break
# printing resultant list of Strings containing any digits
print("List of Strings containing any digits:\n", outputList)
输出
执行上述程序后,将生成以下输出:
Input list: ['hello562', 'tutorialspoint', 'python20', 'codes', '100'] List of Strings containing any digits: ['hello562', 'python20', '100']
结论
从给定的字符串列表中,我们学习了4种不同的提取包含数字的字符串元素的方法。我们还学习了如何根据条件过滤列表。我们还了解到,与其使用嵌套列表,不如在lambda函数中使用any()方法来应用条件。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP