Python去除字典鍵中空格的多種方法
Python是廣泛用于數(shù)據(jù)分析、網(wǎng)頁開發(fā)、人工智能的平臺(tái),并借助自動(dòng)化完成多種不同類型的任務(wù)。了解Python的不同特性對(duì)我們來說非常重要。在本文中,我們將學(xué)習(xí)字典功能以及如何用 Python 去除鍵之間的空格。此功能主要用于根據(jù)需要存儲(chǔ)和檢索數(shù)據(jù),但有時(shí)字典鍵值之間可能存在空隙。這會(huì)導(dǎo)致用戶在想要訪問數(shù)據(jù)時(shí),甚至在需要編輯數(shù)據(jù)時(shí)出現(xiàn)錯(cuò)誤。
去除空格的不同方法
為了確保不會(huì)遇到類似問題并實(shí)現(xiàn)流暢的用戶體驗(yàn),我們可以在字典中刪除鍵之間的空格。那么,在本文中,我們將學(xué)習(xí)如何使用 Python 去除字典鍵中空格的不同方法。
建立新詞典
去除空格的最簡單方法之一是直接創(chuàng)建一個(gè)全新的詞典。步驟是從現(xiàn)有詞典中選擇每個(gè)值對(duì),然后用相同的值創(chuàng)建新詞典,只需去除它們之間的空格。讓我們舉個(gè)例子來更好地理解它:-
def remove_spaces(dictionary):
modified_dictionary = {}
for key, value in dictionary.items():
new_key = key.replace(" ", "") #removing space between keys
modified_dictionary[new_key] = value
return modified_dictionary在上述代碼中
- 字典的輸入被賦予一個(gè)稱為remove_spaces的函數(shù)
- 所有新數(shù)值都已存在于modified_dictionary
- 要使用鍵之間有空格的舊值,可以使用 items()
- 要移除修改后的庫中的所有空格,可以使用 replace() 鍵。
為了檢查鍵之間的空格是否被移除,我們可以使用以下詞典:-
test_dictionary = {"full name": "Aayush Yadav", "age": 12}
#This data is used just for example然后我們將分別使用remove_spaces和test_dictionary作為函數(shù)和參數(shù)
例
def remove_spaces(dictionary):
modified_dictionary = {}
for key, value in dictionary.items():
new_key = key.replace(" ", "") #removing space between keys
modified_dictionary[new_key] = value
return modified_dictionary
test_dictionary = {"full name": "Aayush Yadav", "age": 12}
#This data is used just for example
modified_dictionary = remove_spaces(test_dictionary)
#After the function and argument has been called
print(modified_dictionary)輸出
完成上述所有步驟后,輸出如下:
{'fullname': 'Aayush Yadav', 'age': 12}
全名之間的空格被去除,因此我們成功地去除了空格。
編輯現(xiàn)有詞典
在去除鍵空格的方法下,我們不像第一種方法那樣在移除空格后創(chuàng)建新詞典,而是從現(xiàn)有字典中移除鍵之間的空格。我們將通過以下例子更好地理解它:
def remove_spaces(dictionary):
keys_to_remove = []
for key in dictionary.keys():
if " " in key:
new_key = key.replace(" ", "") #removing space between keys
dictionary[new_key] = dictionary.pop(key)
return dictionary上述代碼可以用來去除鍵之間的所有空格,我們可以通過以下字典作為示例來驗(yàn)證: -
our_dictionary = {"full name": "Aayush Singh" , "Father name": "Yadav"}現(xiàn)在我們將用our_dictionary作為參數(shù),remove_spaces作為函數(shù)來進(jìn)行修改。
例
def remove_spaces(dictionary):
keys_to_remove = []
for key in dictionary.keys():
if " " in key:
new_key = key.replace(" ", "") #removing space between keys
dictionary[new_key] = dictionary.pop(key)
return dictionary
our_dictionary = {"full name": "Aayush Singh" , "Father name": "Yadav"}
remove_spaces(our_dictionary)
print(our_dictionary)輸出
調(diào)用參數(shù)并運(yùn)行函數(shù)后的輸出為:
{'fullname': 'Aayush Singh', 'fathername': 'Yadav'}
全名與父親名字之間的空格被成功消除。
使用詞典理解
這種方法與上述另外兩種方法不同。在這種方法中,我們從詞典理解創(chuàng)建新詞典。鍵值保持不變,但唯一的變化是 rxe中鍵之間的空間在從詞典理解中傳輸?shù)叫略~典時(shí)移動(dòng)了。我們可以用以下代碼更好地理解它:
def no_spaces (dictionary): #From dictionary Comprehension
return {key.replace(" ", ""): value for key, value in dictionary.items()}為了消除鍵值之間的空格,可以使用 replace() 函數(shù),并通過以下字典進(jìn)行檢查:
my_dictionary = {"full name": "Aayush Singh", "father name": "Yadav"}然后我們會(huì)運(yùn)行my_dictionary作為參數(shù),no_spaces作為函數(shù)。
例
def no_spaces (dictionary): #From dictionary Comprehension
return {key.replace(" ", ""): value for key, value in dictionary.items()}
my_dictionary = {"full name": "Aayush Singh", "father name": "Yadav"}
modified_dictionary = no_spaces (my_dictionary)
print(modified_dictionary)輸出
此時(shí)輸出如下:
{'fullname': 'Aayush Singh', 'fathername': 'Yadav'}
全名和父親名字之間的空格被去除,因此我們成功地去除了空格。
遞歸函數(shù)的使用
這種方法最適合當(dāng)一個(gè)詞典存在于另一個(gè)詞典中(嵌套詞典)時(shí)。在這種情況下,我們可以用遞歸函數(shù)去除鍵之間的空格。我們將通過以下例子更好地理解它:
def remove_spaces (dictionary):
new_dictionary = {}
for key, value in dictionary.items(): #For normal dictionary
if isinstance(value, dict): #for nested dictionary
value = remove_spaces (value)
new_key = key.replace(" ", "") #removing space between keys
new_dictionary[new_key] = value
return new_dictionary我們將用以下示例來測(cè)試代碼:
test_dictionary = {"full name": "Aayush Singh", "father name": "Yadav", "details": {"age": 12, "blood group": "O-"}}現(xiàn)在我們將調(diào)用參數(shù) test_dictionary,并運(yùn)行函數(shù) remove_spaces:
例
def remove_spaces (dictionary):
new_dictionary = {}
for key, value in dictionary.items(): #For normal dictionary
if isinstance(value, dict): #for nested dictionary
value = remove_spaces (value)
new_key = key.replace(" ", "") #removing space between keys
new_dictionary[new_key] = value
return new_dictionary
test_dictionary = {"full name": "Aayush Singh", "father name": "Yadav", "details": {"age": 12, "blood group": "O-"}}
modified_dictionary = remove_spaces (test_dictionary)
print(modified_dictionary)輸出
上述示例的輸出為:
{'fullname': 'Aayush Singh', 'fathername': 'Yadav', 'details': {'age': 12, 'bloodgroup': 'O-'}}
全名與父親名之間的空格被移除,因此我們成功地利用遞歸函數(shù)去除了空格。
結(jié)論
Python 有許多不同的用途,很多人會(huì)想用 Python 來減少字典鍵之間的空格。本文介紹了消除鍵之間空格的不同方法。文章包含了所有需要進(jìn)行的編碼,以消除鍵之間的間距,并附有示例以便理解該方法
為了防止運(yùn)行代碼時(shí)出現(xiàn)錯(cuò)誤,確保更改不會(huì)被復(fù)制到代碼的其他部分。
到此這篇關(guān)于Python去除字典鍵中空格的多種方法的文章就介紹到這了,更多相關(guān)Python去除字典鍵空格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用python模塊plotdigitizer摳取論文圖片中的數(shù)據(jù)實(shí)例詳解
這篇文章主要介紹了使用python模塊plotdigitizer摳取論文圖片中的數(shù)據(jù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Python強(qiáng)化練習(xí)之Tensorflow2 opp算法實(shí)現(xiàn)月球登陸器
在面向?qū)ο蟪霈F(xiàn)之前,我們采用的開發(fā)方法都是面向過程的編程(OPP)。面向過程的編程中最常用的一個(gè)分析方法是“功能分解”。我們會(huì)把用戶需求先分解成模塊,然后把模塊分解成大的功能,再把大的功能分解成小的功能,整個(gè)需求就是按照這樣的方式,最終分解成一個(gè)一個(gè)的函數(shù)2021-10-10
Python讀取配置文件-ConfigParser的二次封裝方法
這篇文章主要介紹了Python讀取配置文件-ConfigParser的二次封裝方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Python實(shí)現(xiàn)簡易版的Web服務(wù)器(推薦)
這篇文章主要介紹了Python實(shí)現(xiàn)簡易Web服務(wù)器的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01
利用python實(shí)現(xiàn).dcm格式圖像轉(zhuǎn)為.jpg格式
今天小編就為大家分享一篇利用python實(shí)現(xiàn).dcm格式圖像轉(zhuǎn)為.jpg格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
如何使用django的MTV開發(fā)模式返回一個(gè)網(wǎng)頁
這篇文章主要介紹了如何使用django的MTV開發(fā)模式返回一個(gè)網(wǎng)頁,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07

