Python模糊匹配json对象

问题描述 投票:0回答:1

我有 2 个

lists
json
对象,称为客户和音乐会。我正在尝试实现一个简单的算法来将客户与他们可以参加的音乐会相匹配,并且输出应如下所示。

customer = [
  {
    "name": "John",
    "looking_for": "Rock concert in New York"
  },
  {
    "name": "David",
    "looking_for": "I want to attend a pop concert in Chicago"
  }
]

concert = [
  {
    "name": "Rock",
    "location": "New York",
    "date": "2022-10-10"
  },
  {
    "name": "Pop",
    "location": "Chicago",
    "date": "2022-01-01"
  },
  {
    "name": "Pop",
    "location": "Paris",
    "date": "2023-04-23"
  }
]

有没有一种 Pythonic 方式来获得所需的输出?

Desired output: 
`"John 2022-10-10"
"David 2022-01-01"`
python json fuzzy-search
1个回答
0
投票

你必须学习如何自己编写算法

这个代码会对你有帮助

customer = [
  {
    "name": "John",
    "looking_for": "Rock concert in New York"
  },
  {
    "name": "David",
    "looking_for": "I want to attend a pop concert in Chicago"
  }
]


concert = [
  {
    "name": "Rock",
    "location": "New York",
    "date": "2022-10-10"
  },
  {
    "name": "Pop",
    "location": "Chicago",
    "date": "2022-01-01"
  },
  {
    "name": "Pop",
    "location": "Paris",
    "date": "2023-04-23"
  }
]



for i in concert:

    for j in customer:

        if (i.get("name") in j.get("looking_for")) or (i.get("location") in j.get("looking_for")):
            print("{} {}".format(j.get("name") , i.get("date")))
© www.soinside.com 2019 - 2024. All rights reserved.