Python : List of dict, if exists increment a dict value, if not append a new dict

后端 未结 6 787
悲哀的现实
悲哀的现实 2020-12-04 05:27

I would like do something like that.

list_of_urls = [\'http://www.google.fr/\', \'http://www.google.fr/\', 
                \'http://www.google.cn/\', \'http         


        
6条回答
  •  Happy的楠姐
    2020-12-04 05:51

    This always works fine for me:

    for url in list_of_urls:
        urls.setdefault(url, 0)
        urls[url] += 1
    

提交回复
热议问题