<legend id='vJ1wJ'><style id='vJ1wJ'><dir id='vJ1wJ'><q id='vJ1wJ'></q></dir></style></legend>

    <small id='vJ1wJ'></small><noframes id='vJ1wJ'>

  1. <tfoot id='vJ1wJ'></tfoot>

        <bdo id='vJ1wJ'></bdo><ul id='vJ1wJ'></ul>
      <i id='vJ1wJ'><tr id='vJ1wJ'><dt id='vJ1wJ'><q id='vJ1wJ'><span id='vJ1wJ'><b id='vJ1wJ'><form id='vJ1wJ'><ins id='vJ1wJ'></ins><ul id='vJ1wJ'></ul><sub id='vJ1wJ'></sub></form><legend id='vJ1wJ'></legend><bdo id='vJ1wJ'><pre id='vJ1wJ'><center id='vJ1wJ'></center></pre></bdo></b><th id='vJ1wJ'></th></span></q></dt></tr></i><div id='vJ1wJ'><tfoot id='vJ1wJ'></tfoot><dl id='vJ1wJ'><fieldset id='vJ1wJ'></fieldset></dl></div>

      Python:字典列表,如果存在则增加一个字典值,如果不附加一个新字典

      Python : List of dict, if exists increment a dict value, if not append a new dict(Python:字典列表,如果存在则增加一个字典值,如果不附加一个新字典)
      • <bdo id='Gd7HM'></bdo><ul id='Gd7HM'></ul>
      • <tfoot id='Gd7HM'></tfoot>

      • <i id='Gd7HM'><tr id='Gd7HM'><dt id='Gd7HM'><q id='Gd7HM'><span id='Gd7HM'><b id='Gd7HM'><form id='Gd7HM'><ins id='Gd7HM'></ins><ul id='Gd7HM'></ul><sub id='Gd7HM'></sub></form><legend id='Gd7HM'></legend><bdo id='Gd7HM'><pre id='Gd7HM'><center id='Gd7HM'></center></pre></bdo></b><th id='Gd7HM'></th></span></q></dt></tr></i><div id='Gd7HM'><tfoot id='Gd7HM'></tfoot><dl id='Gd7HM'><fieldset id='Gd7HM'></fieldset></dl></div>

          <small id='Gd7HM'></small><noframes id='Gd7HM'>

            <legend id='Gd7HM'><style id='Gd7HM'><dir id='Gd7HM'><q id='Gd7HM'></q></dir></style></legend>
                <tbody id='Gd7HM'></tbody>
              1. 本文介绍了Python:字典列表,如果存在则增加一个字典值,如果不附加一个新字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想做这样的事情.

                list_of_urls = ['http://www.google.fr/', 'http://www.google.fr/', 
                                'http://www.google.cn/', 'http://www.google.com/', 
                                'http://www.google.fr/', 'http://www.google.fr/', 
                                'http://www.google.fr/', 'http://www.google.com/', 
                                'http://www.google.fr/', 'http://www.google.com/', 
                                'http://www.google.cn/']
                
                urls = [{'url': 'http://www.google.fr/', 'nbr': 1}]
                
                for url in list_of_urls:
                    if url in [f['url'] for f in urls]:
                         urls[??]['nbr'] += 1
                    else:
                         urls.append({'url': url, 'nbr': 1})
                

                我该怎么办?我不知道我是应该拿元组来编辑它还是找出元组索引?

                How can I do ? I don't know if I should take the tuple to edit it or figure out the tuple indices?

                有什么帮助吗?

                推荐答案

                这是一种非常奇怪的组织事物的方式.如果您存储在字典中,这很容易:

                That is a very strange way to organize things. If you stored in a dictionary, this is easy:

                # This example should work in any version of Python.
                # urls_d will contain URL keys, with counts as values, like: {'http://www.google.fr/' : 1 }
                urls_d = {}
                for url in list_of_urls:
                    if not url in urls_d:
                        urls_d[url] = 1
                    else:
                        urls_d[url] += 1
                

                用于更新计数字典的代码是 Python 中的常见模式".这是很常见的,有一个特殊的数据结构,defaultdict,只是为了让这更容易:

                This code for updating a dictionary of counts is a common "pattern" in Python. It is so common that there is a special data structure, defaultdict, created just to make this even easier:

                from collections import defaultdict  # available in Python 2.5 and newer
                
                urls_d = defaultdict(int)
                for url in list_of_urls:
                    urls_d[url] += 1
                

                如果您使用密钥访问 defaultdict,并且该密钥不在 defaultdict 中,则会自动为该密钥添加默认值.defaultdict 获取您传入的可调用对象,并调用它以获取默认值.在这种情况下,我们传入类 int;当 Python 调用 int() 时,它返回一个零值.因此,第一次引用 URL 时,它的计数被初始化为零,然后您将计数加一.

                If you access the defaultdict using a key, and the key is not already in the defaultdict, the key is automatically added with a default value. The defaultdict takes the callable you passed in, and calls it to get the default value. In this case, we passed in class int; when Python calls int() it returns a zero value. So, the first time you reference a URL, its count is initialized to zero, and then you add one to the count.

                但是一个充满计数的字典也是一种常见的模式,所以 Python 提供了一个现成的类: containers.Counter 你只需通过以下方式创建一个 Counter 实例调用类,传入任何可迭代对象;它构建了一个字典,其中键是来自可迭代对象的值,值是键出现在可迭代对象中的次数的计数.上面的例子就变成了:

                But a dictionary full of counts is also a common pattern, so Python provides a ready-to-use class: containers.Counter You just create a Counter instance by calling the class, passing in any iterable; it builds a dictionary where the keys are values from the iterable, and the values are counts of how many times the key appeared in the iterable. The above example then becomes:

                from collections import Counter  # available in Python 2.7 and newer
                
                urls_d = Counter(list_of_urls)
                

                如果您真的需要按照您展示的方式进行操作,最简单和最快的方法是使用这三个示例中的任何一个,然后构建您需要的示例.

                If you really need to do it the way you showed, the easiest and fastest way would be to use any one of these three examples, and then build the one you need.

                from collections import defaultdict  # available in Python 2.5 and newer
                
                urls_d = defaultdict(int)
                for url in list_of_urls:
                    urls_d[url] += 1
                
                urls = [{"url": key, "nbr": value} for key, value in urls_d.items()]
                

                如果您使用的是 Python 2.7 或更新版本,则可以单行执行:

                If you are using Python 2.7 or newer you can do it in a one-liner:

                from collections import Counter
                
                urls = [{"url": key, "nbr": value} for key, value in Counter(list_of_urls).items()]
                

                这篇关于Python:字典列表,如果存在则增加一个字典值,如果不附加一个新字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <tbody id='580ph'></tbody>

                • <small id='580ph'></small><noframes id='580ph'>

                    <bdo id='580ph'></bdo><ul id='580ph'></ul>
                    • <tfoot id='580ph'></tfoot>

                          <legend id='580ph'><style id='580ph'><dir id='580ph'><q id='580ph'></q></dir></style></legend>
                          <i id='580ph'><tr id='580ph'><dt id='580ph'><q id='580ph'><span id='580ph'><b id='580ph'><form id='580ph'><ins id='580ph'></ins><ul id='580ph'></ul><sub id='580ph'></sub></form><legend id='580ph'></legend><bdo id='580ph'><pre id='580ph'><center id='580ph'></center></pre></bdo></b><th id='580ph'></th></span></q></dt></tr></i><div id='580ph'><tfoot id='580ph'></tfoot><dl id='580ph'><fieldset id='580ph'></fieldset></dl></div>