繁体   English   中英

如何获取字典 python 的值?

[英]How to get the values of dictionary python?

我将以下 python 字典存储为dictPython

{
    "paging": {"count": 10, "start": 0, "links": []},
    "elements": [
        {
            "organizationalTarget~": {
                "vanityName": "vv",
                "localizedName": "ViV",
                "name": {
                    "localized": {"en_US": "ViV"},
                    "preferredLocale": {"country": "US", "language": "en"},
                },
                "primaryOrganizationType": "NONE",
                "locations": [],
                "id": 109,
            },
            "role": "ADMINISTRATOR",

        },
    ],
}

我需要获取vanityName, localizedName的值以及来自name->localizedname->preferredLocale的值。

我尝试dictPython.keys()并返回dict_keys(['paging', 'elements'])

我还尝试dictPython.values() ,它返回了括号 ({}) 内的内容。

我需要得到[vv, ViV, ViV, US, en]

我正在以答案的形式写这篇文章,所以我可以在没有评论字符限制的情况下更好地解释它

  • dict中的字典是一种有效的键/值结构或数据类型,例如dict_ = {'key1': 'val1', 'key2': 'val2'}来获取key1我们可以通过两种不同的方式来完成
    1. dict_.get(key1)在这种情况下返回键的值val1 ,此方法有其优点,如果 key1 错误或未找到,则返回None因此不会引发异常。 您可以执行dict_.get(key1, 'returning this string if the key is not found')
    2. dict_['key1']执行相同的.get(...)但如果找不到密钥,则会引发KeyError

因此,为了在介绍之后回答您的问题,可以将dict视为嵌套的字典和/或彼此内部的对象以获得您的值,您可以执行以下操作

# Fetch base dictionary to make code more readable
base_dict = dict_["elements"][0]["organizationalTarget~"]
# fetch name_dict following the same approach as above code
name_dict = base_dict["name"]
localized_dict = name_dict["localized"]
preferred_locale_dict = name_dict ["preferredLocale"]

所以现在我们从给定的字典中获取相应位置的所有想要的数据,现在要打印结果,我们可以执行以下操作

results_arr = []
for key1, key2 in zip(localized_dict, preferredLocale_dict):
    results_arr.append(localized_dict.get(key1))
    results_arr.append(preferred_locale_dict.get(key2))

print(results_arr)

关于什么:

dic = {
    "paging": {"count": 10, "start": 0, "links": []},
    "elements": [
        {
            "organizationalTarget~": {
                "vanityName": "vv",
                "localizedName": "ViV",
                "name": {
                    "localized": {"en_US": "ViV"},
                    "preferredLocale": {"country": "US", "language": "en"},
                },
                "primaryOrganizationType": "NONE",
                "locations": [],
                "id": 109,
            },
            "role": "ADMINISTRATOR",
        },
    ],
}
base = dic["elements"][0]["organizationalTarget~"]
c = base["name"]["localized"]
d = base["name"]["preferredLocale"]
output = [base["vanityName"], base["localizedName"]]
output.extend([c[key] for key in c])
output.extend([d[key] for key in d])

print(output)

输出:

['vv', 'ViV', 'ViV', 'US', 'en']

那么像这样的东西?

[[x['organizationalTarget~']['vanityName'],
  x['organizationalTarget~']['localizedName'],
  x['organizationalTarget~']['name']['localized']['en_US'],
  x['organizationalTarget~']['name']['preferredLocale']['country'],
  x['organizationalTarget~']['name']['preferredLocale']['language'],
 ] for x in s['elements']]

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM