简体   繁体   English

如何在 Python 的嵌套字典中用其对应的 Boolean 值替换字符串值?

[英]How can I replace a string value with its corresponding Boolean value in a nested dictionary in Python?

I was recently given a project to do for my Python college course.我最近得到了一个项目来完成我的 Python 大学课程。 Basically, you save a dictionary in a csv file with csv.DictWriter, and then you load it back into another dictionary with csv.DictReader.基本上,您使用 csv.DictWriter 将字典保存在 csv 文件中,然后使用 csv.DictReader 将其加载回另一个字典。 However, for some reason, one of the automated unittests fails my function because the final dictionary contains values "True" or "False" instead of Bool values True or False (which I just consider to be logical, given we literally read a string from a file and placed it into a dictionary).但是,出于某种原因,我的 function 中的一个自动单元测试失败了,因为最终字典包含值“True”或“False”,而不是 Bool 值 True 或 False(我认为这是合乎逻辑的,因为我们从字面上读取了一个字符串一个文件并将其放入字典中)。

Anyways, I've been toying around with the idea of actually replacing these before my function returns the dict, but even the version of the code that somehow works in terms of syntax is not functional logically, and the True and False values remain strings.无论如何,在我的 function 返回 dict 之前,我一直在考虑实际替换这些的想法,但即使是在语法方面以某种方式工作的代码版本在逻辑上也不起作用,并且 True 和 False 值仍然是字符串。 Here's an example of what I tried:这是我尝试过的示例:

a = {
    "b": {
        "name": "gari",
        "last_name": "jovan",
        "truth": "True"
    },

    "c": {
        "name": "miki",
        "last_name": "milane",
        "truth": "False"
    },

    "d": {
        "name": "baki",
        "last_name": "mico",
        "truth": "True"
    }
}

for i in a: #we approach individual small dicts
    for j in i: #we approach keys in said dicts
        if j == "truth":
            if j["truth"] == "False":
                j["truth"] == False
            elif j["truth"] == "True":
                j["truth"] == True
            else:
                raise Exception("Greska")
        else:
            pass
    else:
        pass

print(a)

As I said, this is just a contained example, not something from my actual project, because I tried figuring the actual solution through an isolated piece of code.正如我所说,这只是一个包含的示例,而不是我实际项目中的示例,因为我试图通过一段孤立的代码来计算实际的解决方案。 Still, this doesn't seem to work, and I was just wondering if anyone would be able to help me out.尽管如此,这似乎不起作用,我只是想知道是否有人能够帮助我。

Thanks in advance!提前致谢!

Try:尝试:

a = {
    "b": {"name": "gari", "last_name": "jovan", "truth": "True"},
    "c": {"name": "miki", "last_name": "milane", "truth": "False"},
    "d": {"name": "baki", "last_name": "mico", "truth": "True"},
}


for d in a.values():
    d["truth"] = d["truth"] == "True"

print(a)

Prints:印刷:

{
    "b": {"name": "gari", "last_name": "jovan", "truth": True},
    "c": {"name": "miki", "last_name": "milane", "truth": False},
    "d": {"name": "baki", "last_name": "mico", "truth": True},
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何 plot 一个 Python 字典键与其对应的字典值? - How do I plot a Python dictionary key against its corresponding dictionary value? 如何通过具有相应键的字典值更改列中的值? Python - How I can change value in column by a value of dictionary with corresponding key ? Python 如何遍历python字典并替换子字符串值? - How can I loop through a python dictionary and replace a substring value? 如何更改嵌套字典键的 int 值? (Python 3) - How can I change int value of nested dictionary key? (Python 3) 如何在 python 中将嵌套列表作为字典值进行迭代和排序? - How can i iterate and order nested list as a dictionary value in python? 如何按字典值对字典排序? - How can I sort the dictionary by its value? Python tkinter combobox select 字典键如何打印对应值? - Python tkinter combobox, how can I select dictionary key and print corresponding value? 如何将键中的嵌套python字典值替换为以点分隔的字符串格式? - How to replace nested python dictionary value from a key as a string format with separated by dots? 如何确认一个值是否在嵌套字典中 - How can I confirm a value is in nested dictionary or not 如何从python字典中提取相应的值? - How to extract corresponding value from python dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM