【问题标题】:how can I checking only a spefic dict key for unique values?如何仅签入特定 dict 键的唯一值?
【发布时间】:2019-11-04 09:09:15
【问题描述】:

我正在尝试相同的行为,但我想检查 url dict 是否包含唯一值,而不是检查完整的 dict。

def unique(data):
    result = {}
    for key, value in data.items():
        new_list = []
        for item in value:
            if item not in new_list:
                new_list.append(item)
        result[key] = new_list
        return result



games = { 
   "games":[ 
      { 
         "city":"Santa Monica, CA",
         "company":"rockstar",
         "date":{ 
            "display":"24 days ago",
            "timestamp":1570721082
         },
         "game":"akakkak",
         "challenge":"leel",
         "url":"https://www.game.com/12757a575/aaashs/2222"
      },{ 
         "city":"Santa Monica, CA",
         "company":"rockstar",
         "date":{ 
            "display":"24 days ago",
            "timestamp":1570721082
         },
         "game":"akak2kak",
         "challenge":"leel",
         "url":"https://www.game.com/12757a575/aaashs/2222"
      }
   ]
}

print(unique(games))

我希望"url":"https://www.game.com/12757a575/aaashs/2222" 的输出在同一个键中没有相同的值

【问题讨论】:

标签: python


【解决方案1】:

如何创建一个单独的 url 列表来跟踪每个唯一的 url,如下所示:

def unique(data):
    result = {}
    for key, value in data.items():
        new_list = []
        url_list = []
        for item in value:
            if item['url'] not in url_list:
                new_list.append(item)
                url_list.append(item['url'])
        result[key] = new_list
        return result

【讨论】:

  • 我认为检查 dict 的所有内容会保持相同的行为,但我只想检查 url 键值
猜你喜欢
  • 1970-01-01
  • 2012-09-05
  • 2019-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多