简体   繁体   English

如何迭代 2 JSON 列表(混合字典和列表),匹配特定值并打印另一个值

[英]How do I iterate over 2 JSON lists (mix dict and list), match specific values and print another value

I have 2 separate JSON-Lists (with dicts) in it.我有 2 个单独的 JSON 列表(带有字典)。 My goal is, that I want to iterate over list2 "currentUser", grab the values, search those values in list1, and as output print the value of "firstName"我的目标是,我想遍历 list2“currentUser”,获取值,在 list1 中搜索这些值,然后 output 打印“firstName”的值

eg liste2: "currentUser": 123, liste1: "id": "123", --> "firstName": "Lisa",例如 liste2: "currentUser": 123, liste1: "id": "123", --> "firstName": "Lisa",

list1 = {
    "X-API-KEY": "XyZzahZaksksXXXYYYOOO000",
    "user": {
                "email": "Lisa@BLA.com",
                "firstName": "Lisa",
                "id": "123",
    },
    "Flat": {
        "city": "Munich",
        "country": "2",
        "countryCode": "DEU",
        "currency": "EUR",
        "date": "1587671397",
        "flatmates": [
            {
                "email": "Lisa@BLA.com",
                "firstName": "Lisa",
                "id": "123",
            },
            {
                "email": "Max@BLA.com",
                "firstName": "Max",
                "id": "124",
            },
            {
                "email": "Hannah@BLA.com",
                "firstName": "Hannah",
                "id": "125",
            },
            {
                "email": "Kai@BLA.com",
                "firstName": "Kai",
                "id": "126",
            }
        ],
        "founderId": "123",
        "id": "99999",
        "image": "",
        "name": "ABC",
        "postCode": "000000",
    }
}
list2 = [
    {
        "creationDate": 1587671663,
        "currentUser": 123,
        "id": 1717134,
        "title": "Do this",
        "users": [
            124,
            126
        ]
    },
    {
        "creationDate": 1587671663,
        "currentUser": 126,
        "id": 1717134,
        "title": "Do that",
        "users": [
            123,
            125
        ]
    },
    {
        "creationDate": 1587671821,
        "currentUser": 124,
        "id": 1717134,
        "title": "Clean this",
        "users": [
            125,
            122
        ]
    },
    {
        "creationDate": 1587671801,
        "currentUser": 123,
        "id": 1717134,
        "title": "Clean that",
        "users": [
            124,
            126
        ]
    }
]

I am pretty new to python. There are several mind-issues for me since there is a mix between lists and dictionaries in it and how to match/search for values for 2 separate lists/dicts我是 python 的新手。我有几个头脑问题,因为它混合了列表和字典,以及如何匹配/搜索 2 个单独的列表/字典的值

What I got so far: Iterate over the "CurrentUser"到目前为止我得到了什么:迭代“CurrentUser”

for user in liste2:
    print(user["currentUser"])

Has anyone some approaches?有没有人有一些方法?

In pure python with no other modules.在没有其他模块的纯 python 中。

for user in list2:
    for mate in list1['Flat']['flatmates']:
        if user['currentUser'] == int(mate['id']):
            # You found the person now execute this code...

One thing to note that in your list1 you flatmates id is not a integer but a string.需要注意的一件事是,在您的 list1 中,您的室友 ID 不是 integer 而是一个字符串。 So you have to convert that to an int in order to compare the two.因此,您必须将其转换为 int 才能比较两者。

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

相关问题 django模板中如何遍历此特定数据(列表中的dict是另一个列表中的元组的第二个值)? - How could iterate over this specific data (dict inside a list which is the second value of a tuple inside another list) in a django template? 在遍历包含嵌套字典和列表的字典列表时,如何选择特定值? - How do I pick specific values as I iterate through a list of dictionaries that contain nested dictionaries and lists? 遍历存储在字典值中的列表 - Iterate over lists stored in dict values 如果某些键具有所需值,如何迭代字典列表并打印字典 - How to Iterate over a list of dictionary and print dict if certain key has required value 如何遍历值对列表以将各自的值设置为等于新值列表? - How do I iterate over a list of value pairs to set the respective value equal to a list of new values? 如何遍历列表列表? - How can I iterate over a list of lists? 如何将列表与字典和时间相关值匹配? - How do I match a list with a dict and time related values? 如何打印或迭代链接列表的链接列表的对象? - How to print or iterate over an object of a linked list of linked lists? 如何迭代并修改dict中的值? - How can I iterate over and modify the values in a dict? 如何遍历 dict 结构中的列表? - How can I iterate over a list within a dict structure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM