python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python去除字典键空格

Python去除字典键中空格的多种方法

作者:小鸡吃米…

本文介绍了Python中去除字典键空格的多种方法,文章中描述了四种不同的方法,包括建立新词典、编辑现有词典、使用词典理解以及递归函数,感兴趣的可以了解一下

Python是广泛用于数据分析、网页开发、人工智能的平台,并借助自动化完成多种不同类型的任务。了解Python的不同特性对我们来说非常重要。在本文中,我们将学习字典功能以及如何用 Python 去除键之间的空格。此功能主要用于根据需要存储和检索数据,但有时字典键值之间可能存在空隙。这会导致用户在想要访问数据时,甚至在需要编辑数据时出现错误。

去除空格的不同方法

为了确保不会遇到类似问题并实现流畅的用户体验,我们可以在字典中删除键之间的空格。那么,在本文中,我们将学习如何使用 Python 去除字典键中空格的不同方法。

建立新词典

去除空格的最简单方法之一是直接创建一个全新的词典。步骤是从现有词典中选择每个值对,然后用相同的值创建新词典,只需去除它们之间的空格。让我们举个例子来更好地理解它:-

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

然后我们将分别使用remove_spaces和test_dictionary作为函数和参数

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}

全名之间的空格被去除,因此我们成功地去除了空格。

编辑现有词典

在去除键空格的方法下,我们不像第一种方法那样在移除空格后创建新词典,而是从现有字典中移除键之间的空格。我们将通过以下例子更好地理解它:

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"}

现在我们将用our_dictionary作为参数,remove_spaces作为函数来进行修改。

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)

输出

调用参数并运行函数后的输出为:

{'fullname': 'Aayush Singh', 'fathername': 'Yadav'}

全名与父亲名字之间的空格被成功消除。

使用词典理解

这种方法与上述另外两种方法不同。在这种方法中,我们从词典理解创建新词典。键值保持不变,但唯一的变化是 rxe中键之间的空间在从词典理解中传输到新词典时移动了。我们可以用以下代码更好地理解它:

def no_spaces (dictionary): #From dictionary Comprehension
    return {key.replace(" ", ""): value for key, value in dictionary.items()}

为了消除键值之间的空格,可以使用 replace() 函数,并通过以下字典进行检查:

my_dictionary = {"full name": "Aayush Singh", "father name": "Yadav"}

然后我们会运行my_dictionary作为参数,no_spaces作为函数。

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)

输出

此时输出如下:

{'fullname': 'Aayush Singh', 'fathername': 'Yadav'}

全名和父亲名字之间的空格被去除,因此我们成功地去除了空格。

递归函数的使用

这种方法最适合当一个词典存在于另一个词典中(嵌套词典)时。在这种情况下,我们可以用递归函数去除键之间的空格。我们将通过以下例子更好地理解它:

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-"}}

现在我们将调用参数 test_dictionary,并运行函数 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-'}}

全名与父亲名之间的空格被移除,因此我们成功地利用递归函数去除了空格。

结论

Python 有许多不同的用途,很多人会想用 Python 来减少字典键之间的空格。本文介绍了消除键之间空格的不同方法。文章包含了所有需要进行的编码,以消除键之间的间距,并附有示例以便理解该方法

为了防止运行代码时出现错误,确保更改不会被复制到代码的其他部分。

到此这篇关于Python去除字典键中空格的多种方法的文章就介绍到这了,更多相关Python去除字典键空格内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文