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

      <bdo id='KSjYt'></bdo><ul id='KSjYt'></ul>

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

        独特词典列表

        List of unique dictionaries(独特词典列表)

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

          <tbody id='ptmHI'></tbody>

        <tfoot id='ptmHI'></tfoot>
          <bdo id='ptmHI'></bdo><ul id='ptmHI'></ul>
          • <legend id='ptmHI'><style id='ptmHI'><dir id='ptmHI'><q id='ptmHI'></q></dir></style></legend>

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

                1. 本文介绍了独特词典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有一个字典列表:

                  [
                      {'id': 1, 'name': 'john', 'age': 34},
                      {'id': 1, 'name': 'john', 'age': 34},
                      {'id': 2, 'name': 'hanna', 'age': 30},
                  ]
                  

                  如何获取唯一词典列表(删除重复项)?

                  [
                      {'id': 1, 'name': 'john', 'age': 34},
                      {'id': 2, 'name': 'hanna', 'age': 30},
                  ]
                  

                  推荐答案

                  因此使用密钥id创建一个临时字典。这会过滤掉重复项。 词典的values()将是列表

                  在Python2.7中

                  >>> L=[
                  ... {'id':1,'name':'john', 'age':34},
                  ... {'id':1,'name':'john', 'age':34},
                  ... {'id':2,'name':'hanna', 'age':30},
                  ... ]
                  >>> {v['id']:v for v in L}.values()
                  [{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]
                  

                  在Python3中

                  >>> L=[
                  ... {'id':1,'name':'john', 'age':34},
                  ... {'id':1,'name':'john', 'age':34},
                  ... {'id':2,'name':'hanna', 'age':30},
                  ... ] 
                  >>> list({v['id']:v for v in L}.values())
                  [{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]
                  

                  在Python2.5/2.6中

                  >>> L=[
                  ... {'id':1,'name':'john', 'age':34},
                  ... {'id':1,'name':'john', 'age':34},
                  ... {'id':2,'name':'hanna', 'age':30},
                  ... ] 
                  >>> dict((v['id'],v) for v in L).values()
                  [{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]
                  

                  这篇关于独特词典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                  • <bdo id='nVChp'></bdo><ul id='nVChp'></ul>

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

                    2. <small id='nVChp'></small><noframes id='nVChp'>