<tfoot id='O3ALG'></tfoot>
  • <legend id='O3ALG'><style id='O3ALG'><dir id='O3ALG'><q id='O3ALG'></q></dir></style></legend>

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

    1. <small id='O3ALG'></small><noframes id='O3ALG'>

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

        字典通过更新合并,但如果值存在则不覆盖

        Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
      1. <legend id='W7xt5'><style id='W7xt5'><dir id='W7xt5'><q id='W7xt5'></q></dir></style></legend>

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

          <tfoot id='W7xt5'></tfoot>

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

                  本文介绍了字典通过更新合并,但如果值存在则不覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  If I have 2 dicts as follows:

                  d1 = {'a': 2, 'b': 4}
                  d2 = {'a': 2, 'b': ''}
                  

                  In order to 'merge' them:

                  dict(d1.items() + d2.items())
                  

                  results in

                  {'a': 2, 'b': ''}
                  

                  But what should I do if I would like to compare each value of the two dictionaries and only update d2 into d1 if values in d1 are empty/None/''?

                  When the same key exists, I would like to only maintain the numerical value (either from d1 or d2) instead of the empty value. If both values are empty, then no problems maintaining the empty value. If both have values, then d1-value should stay.

                  i.e.

                  d1 = {'a': 2, 'b': 8, 'c': ''}
                  d2 = {'a': 2, 'b': '', 'c': ''}
                  

                  should result in

                  {'a': 2, 'b': 8, 'c': ''}
                  

                  where 8 is not overwritten by ''.

                  解决方案

                  Just switch the order:

                  z = dict(d2.items() + d1.items())
                  

                  By the way, you may also be interested in the potentially faster update method.

                  In Python 3, you have to cast the view objects to lists first:

                  z = dict(list(d2.items()) + list(d1.items())) 
                  

                  If you want to special-case empty strings, you can do the following:

                  def mergeDictsOverwriteEmpty(d1, d2):
                      res = d2.copy()
                      for k,v in d2.items():
                          if k not in d1 or d1[k] == '':
                              res[k] = v
                      return res
                  

                  这篇关于字典通过更新合并,但如果值存在则不覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

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

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

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

                        <tbody id='dzJKa'></tbody>
                        <bdo id='dzJKa'></bdo><ul id='dzJKa'></ul>