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