• <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'>

        独特词典列表

        时间:2024-04-21

        <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'}]
                  

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

                  上一篇:如果字典键不可用,则返回默认值 下一篇:如何制作一个字典,为字典中缺少的键返回键,而不是引发KeyError?

                  相关文章

                  • <bdo id='nVChp'></bdo><ul id='nVChp'></ul>

                  <tfoot id='nVChp'></tfoot>
                  1. <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>
                  2. <legend id='nVChp'><style id='nVChp'><dir id='nVChp'><q id='nVChp'></q></dir></style></legend>

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