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

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

    1. <legend id='SM1nQ'><style id='SM1nQ'><dir id='SM1nQ'><q id='SM1nQ'></q></dir></style></legend><tfoot id='SM1nQ'></tfoot>
    2. 将集合转换为字符串,反之亦然

      时间:2023-07-03
      <legend id='jupbZ'><style id='jupbZ'><dir id='jupbZ'><q id='jupbZ'></q></dir></style></legend>

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

      <tfoot id='jupbZ'></tfoot>

                <tbody id='jupbZ'></tbody>
              <i id='jupbZ'><tr id='jupbZ'><dt id='jupbZ'><q id='jupbZ'><span id='jupbZ'><b id='jupbZ'><form id='jupbZ'><ins id='jupbZ'></ins><ul id='jupbZ'></ul><sub id='jupbZ'></sub></form><legend id='jupbZ'></legend><bdo id='jupbZ'><pre id='jupbZ'><center id='jupbZ'></center></pre></bdo></b><th id='jupbZ'></th></span></q></dt></tr></i><div id='jupbZ'><tfoot id='jupbZ'></tfoot><dl id='jupbZ'><fieldset id='jupbZ'></fieldset></dl></div>
                <bdo id='jupbZ'></bdo><ul id='jupbZ'></ul>
                本文介绍了将集合转换为字符串,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                设置为字符串.很明显:

                Set to string. Obvious:

                >>> s = set([1,2,3])
                >>> s
                set([1, 2, 3])
                >>> str(s)
                'set([1, 2, 3])'
                

                要设置的字符串?也许是这样?

                String to set? Maybe like this?

                >>> set(map(int,str(s).split('set([')[-1].split('])')[0].split(',')))
                set([1, 2, 3])
                

                非常丑陋.有没有更好的方法来序列化/反序列化集合?

                Extremely ugly. Is there better way to serialize/deserialize sets?

                推荐答案

                使用 repreval:

                >>> s = set([1,2,3])
                >>> strs = repr(s)
                >>> strs
                'set([1, 2, 3])'
                >>> eval(strs)
                set([1, 2, 3])
                

                请注意,如果字符串的来源未知,eval 是不安全的,为了更安全的转换,首选 ast.literal_eval:

                Note that eval is not safe if the source of string is unknown, prefer ast.literal_eval for safer conversion:

                >>> from ast import literal_eval
                >>> s = set([10, 20, 30])
                >>> lis = str(list(s))
                >>> set(literal_eval(lis))
                set([10, 20, 30])
                

                repr 的帮助:

                repr(object) -> string
                Return the canonical string representation of the object.
                For most object types, eval(repr(object)) == object.
                

                这篇关于将集合转换为字符串,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Python 设置的无序是否可以被视为随机序? 下一篇:Python中set.discard和set.remove方法之间的运行时差异?

                相关文章

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

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

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