python - Python 中的另一个嵌套字典理解

标签 python dictionary

我一直无法理解如何用其他嵌套 python 字典示例中出现的术语来理解我的问题,所以我发布了一个新问题,希望得到具体答案!

问题一: 我想了解是否可以使用传统的“for key, item in dict.items()”字典理解来获取我字典中的值。

testdict = dict(
[('PERSON 1',
  {'bonus': 600000,
   'deferral_payments': 'NaN',
   'email_address': 'person.1@something.com',
   'total_stock_value': 585062}),
 ('PERSON 2',
  {'bonus': 'NaN',
   'deferral_payments': 1295738,
   'email_address': 'NaN',
   'total_stock_value': 10623258})
   ])

# This works in the desired way, it unpacks the individual items of the nested dictionary rows...

for key in testdict.keys():
    for item in testdict[key]:
        print("{0}, {1}, {2}".format(key, item, testdict[key][item]))

#PERSON 1, bonus, 600000
#PERSON 1, deferral_payments, NaN
#PERSON 1, email_address, person.1@something.com
#PERSON 1, total_stock_value, 585062
#PERSON 2, bonus, NaN
#PERSON 2, deferral_payments, 1295738
#PERSON 2, email_address, NaN
#PERSON 2, total_stock_value, 10623258

# Trying the traditional approach using key, item in dict.items()

for key, item in testdict.items():
    print("{0}, {1}".format(key, item))

    # If I try print(testdict[key][item]) here I get the error "TypeError: unhashable type: 'dict'"

#PERSON 1, {'bonus': 600000, 'deferral_payments': 'NaN', 'email_address': 'person.1@something.com', 'total_stock_value': 585062}
#PERSON 2, {'bonus': 'NaN', 'deferral_payments': 1295738, 'email_address': 'NaN', 'total_stock_value': 10623258}

使用 key, item in dict.items() 的传统方法我无法得到相同的结果 - 我该如何正确使用这种方法?


问题二: 有什么方法可以在我的代码示例中(或在我对问题 1 的请求的任何解决方案中)包含一个 if 子句?我的嵌套字典中有一些键应该有数值但目前有 NaN,我可以通过以下方式找到这些键,但是有什么方法可以将条件子句作为循环的一部分包含在内吗?

value_keys = ["bonus", 
              "deferral_payments", 
              "total_stock_value"]

for key in testdict.keys():
    for item in testdict[key]:
        if item in value_keys and testdict[key][item] == "NaN":
            print("{0}, {1}, {2}".format(key, item, testdict[key][item]))

#PERSON 1, deferral_payments, NaN
#PERSON 2, bonus, NaN

最佳答案

对于你的第一个问题,无论你做什么,你都必须迭代外部字典,然后再迭代每个内部字典。希望这有助于:

>>> for outer_key, outer_value in testdict.items():
...     for inner_key, inner_value in outer_value.items():
...         print("{0}, {1}, {2}".format(outer_key, inner_key, inner_value))
...
PERSON 1, bonus, 600000
PERSON 1, deferral_payments, NaN
PERSON 1, email_address, person.1@something.com
PERSON 1, total_stock_value, 585062
PERSON 2, bonus, NaN
PERSON 2, deferral_payments, 1295738
PERSON 2, email_address, NaN
PERSON 2, total_stock_value, 10623258

我不明白你的第二个问题,但无论如何你都应该把它作为一个单独的问题来问。 (Stack Overflow 上的一个“问题”实际上是两个不相关的问题,对 future 的访问者来说不是很有用。)

关于python - Python 中的另一个嵌套字典理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46354972/

相关文章:

python - mypy:创建一个接受子类实例列表的类型

python - 随机选择一个点,但没有密度偏差

python - Python中将两个字典合并为字典的字典

c# - 将列表创建的字典从 Python 转换为 C#

ios - 如何在 react-native map 上放置多个标记?

python - 我可以根据字典的键吸收字典的值吗?

python - ipython和jupyter console有什么关系和区别

python - 在 python 中使用 pandas 合并行数据

python - 有没有办法将 python 程序刻录到光盘?

c# - 快速将多个对象映射到另一个对象的数据结构