Python list of dictionaries search

后端 未结 21 2053
-上瘾入骨i
-上瘾入骨i 2020-11-22 09:41

Assume I have this:

[
{\"name\": \"Tom\", \"age\": 10},
{\"name\": \"Mark\", \"age\": 5},
{\"name\": \"Pam\", \"age\": 7}
]

and by searchin

21条回答
  •  情深已故
    2020-11-22 10:23

    people = [
    {'name': "Tom", 'age': 10},
    {'name': "Mark", 'age': 5},
    {'name': "Pam", 'age': 7}
    ]
    
    def search(name):
        for p in people:
            if p['name'] == name:
                return p
    
    search("Pam")
    

提交回复
热议问题