<tfoot id='JPUWk'></tfoot>

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

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

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

    1. 如何将值附加到 dict 键?(AttributeError:'str'对象没有属性'appen

      时间:2023-09-27

        <tfoot id='jZCmF'></tfoot><legend id='jZCmF'><style id='jZCmF'><dir id='jZCmF'><q id='jZCmF'></q></dir></style></legend>

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

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

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

              • 本文介绍了如何将值附加到 dict 键?(AttributeError:'str'对象没有属性'append')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我有一本带有一个键(和一个值)的字典:

                Say I have a dictionary with one key (and a value):

                dict = {'key': '500'}.
                

                现在我想向同一个键添加一个新值 '1000'.然而,

                Now I want to add a new value '1000' to the same key. However,

                dict[key].append('1000')
                

                只给我 AttributeError: 'str' object has no attribute 'append'".

                如果我这样做了

                dict[key] = '1000' 
                

                它替换了之前的值.

                我猜我必须创建一个列表作为值,并以某种方式将该列表附加为键的值,但我不确定我将如何处理.感谢您的帮助!

                I'm guessing I have to create a list as a value and somehow append that list as the key's value but I'm not sure how I would go about this. Thanks for any help!

                推荐答案

                我建议使用 defaultdict 在缺少键时实例化一个空列表.

                I suggest the usage of a defaultdict that instantiates an empty list when a key is missing.

                >>> from collections import defaultdict
                >>> d = defaultdict(list)
                >>> d['key'].append(500)
                >>> d
                defaultdict(<type 'list'>, {'key': [500]})
                >>> d['key'].append(1000)
                >>> d
                defaultdict(<type 'list'>, {'key': [500, 1000]})
                

                我不建议将字符串/整数作为值,然后在您想附加到字段时切换到列表.保持一致.

                I don't recommend having strings/integers as values and then switching to lists once you want to append to a field. Keep it consistent.

                这篇关于如何将值附加到 dict 键?(AttributeError:'str'对象没有属性'append')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:将列表元素附加到python中的列表列表 下一篇:append 和 insert 都存在有什么原因吗?

                相关文章

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

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