如何使用 Python 從字串列表中刪除所有數字?


列表資料結構儲存著不同資料型別元素,例如整數、字串或浮點數。一旦在列表資料結構(方括號內)中定義了元素,就不能更改。字串值中定義的數字可以被移除,然後只返回不包含數字的字串值。本文將介紹如何從列表資料結構中定義的每個字串中移除數字,並提供字串操作技巧。

從字串列表中刪除所有數字

從字串中移除整數,並從簡單的操作到複雜的功能進行介紹。字串由字元組成,僅列印不含整數的字串涉及一些複雜的過程。

方法

  • 方法 1 - 使用 replace 模組

  • 方法 2 - 使用 re 模組

  • 方法 3 - 使用 isalpha() 方法

方法 1:使用 replace 模組從字串列表中刪除所有數字的 Python 程式

replace 模組用於將宣告的變數中的整數替換為空格。

演算法

  • 步驟 1 - 使用一個輸入字串引數定義函式。

  • 步驟 2 - 使用列表推導式替換每個元素。

  • 步驟 3 - 使用字串變數初始化輸入,並將其分配給 string1 變數,同時包含整數。

  • 步驟 4 - 列印列表輸出,不包含整數。

示例

#defining the remove integer function with one argument 
def removeinteger(stringlist):
	return [a.replace('0', '').replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', ''). replace('6', ''). replace('7', ''). replace('8', ''). replace('9', '') for a in stringlist]
#initializing the list of strings
string1 = ['John53mass', '66elsa98Marvel', '300perfect04stay']
#the newlist holds the string after removing the integers
newlist = removeinteger(string1)
#Print the function 
print("The list of string after integer removal:", newlist)

輸出

The list of string after integer removal: ['Johnmass', 'elsaMarvel', 'perfectstay']

方法 2:使用 re 模組從字串列表中刪除所有數字的 Python 程式

re 模組用於移除給定字串列表中的所有整數,然後將所有數字替換為字串。

演算法

  • 步驟 1 - 使用字元和整數一起宣告輸入變數,例如 John53mass。

  • 步驟 2 - 該程式透過從字串中移除數字 (53) 來工作。

  • 步驟 3 - 定義名為 removeinteger() 的函式,該函式帶有一個引數,並匯入 re 模組以使用一些內建函式。

  • 步驟 4 - 使用 re 模組中的 re.sub() 函式查詢字串,然後將它們替換為整數值。

  • 步驟 5 - 建立 newlist 來儲存結果。

  • 步驟 6 - for 迴圈將根據列表長度遍歷列表。

  • 步驟 7 - 最後,列印輸出。

示例

#importing the required module
import re
#function defined with one argument
def removeint(val):
	return re.sub("\d+", "", val)
#initializing the list of strings with numbers inside string
string1 = ['John53mass', '66elsa98Marvel', '300perfect04stay']
#initializing the empty list
newlist = []
#for loop is used to iterate through the list along with the len function
for a in range(len(string1)):
	newlist.append(removeint(string1[a]))
#returning the list after removing the integers
print("The list of string after integer removal:", newlist)

輸出

The list of string after integer removal: ['Johnmass', 'elsaMarvel', 'perfectstay']

方法 3:使用 isalpha() 方法從字串列表中刪除所有數字的 Python 程式

使用 isalpha() 方法從元素列表中移除整數後,將列印列表。

演算法

  • 步驟 1 - 輸入可以是任何包含數字的字串,輸出將列印不包含這些數字的字串。

  • 步驟 2 - 宣告一個空列表並將其分配給 empty 變數。

  • 步驟 3 - 使用列表推導式,我們可以根據給定列表的長度遍歷元素。

  • 步驟 4 - 使用 join 函式追加字母,然後儲存在空列表中。

  • 步驟 5 - 列印最終列表,字串中不包含數字。

示例

#initializing the list data structure with string and integer elements along with integer values
string1 = ['John53mass', '66elsa98Marvel', '300perfect04stay']
#declaring an empty list
empty = []
#for loop is used to iterate through the list by ranging based on the length
for a in range(len(string1)):
	empty.append(''.join(e for e in string1[a] if e.isalpha()))
#printing the list after removing the integers from the list 
print("The list of string after integer removal:", empty)

輸出

The list of string after integer removal: ['Johnmass', 'elsaMarvel', 'perfectstay']

結論

Python 用於各種技術,例如機器學習和人工智慧。這些方法幫助程式設計師在模組和列表推導式的幫助下從字串列表中移除整數。資料處理是當前技術中最突出的一個方面,從宣告的輸入字串中移除整數是基本功能之一。

更新於: 2023-09-04

264 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.