1

Есть строка:

{
  "success": "true",
  "date": "0",
  "base": [ 
    {"item": "marker", "color": "red", "permanent": "yes"}, 
    {"item": "marker", "color": "blue", "permanent": "no"},
    {"item": "marker", "color": "green", "permanent": "yes"},
    {"item": "pen", "color": "black", "permanent": "yes"},
    {"item": "pen", "color": "white", "permanent": "yes"},
    {"item": "pen", "color": "grey", "permanent": "yes"}
  ]
}

Необходимо отфильтровать по двум значениям: "item" = "marker", "permanent" = "yes". Буду благодарен за помощь.

1 ответ 1

3
dct = {
  "success": "true",
  "date": "0",
  "base": [ 
    {"item": "marker", "color": "red", "permanent": "yes"}, 
    {"item": "marker", "color": "blue", "permanent": "no"},
    {"item": "marker", "color": "green", "permanent": "yes"},
    {"item": "pen", "color": "black", "permanent": "yes"},
    {"item": "pen", "color": "white", "permanent": "yes"},
    {"item": "pen", "color": "grey", "permanent": "yes"}
  ]
}
print(list(filter(lambda d: d['item'] == 'marker' and d['permanent'] == 'yes', dct['base'])))

[{'item': 'marker', 'color': 'red', 'permanent': 'yes'}, {'item': 'marker', 'color': 'green', 'permanent': 'yes'}]

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.