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

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

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

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

      1. python中将列表转换为元组的时间复杂度,反之亦然

        时间:2023-08-31
          <tbody id='ETgir'></tbody>

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

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

                2. 本文介绍了python中将列表转换为元组的时间复杂度,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  将python列表转换为元组的时间复杂度是多少(反之亦然):

                  what is the time complexity of converting a python list to tuple (and vice versa):

                  tuple([1,2,3,4,5,6,42])
                  list((10,9,8,7,6,5,4,3,1))
                  

                  O(N) 或 O(1),即列表是否被复制或内部某处从可写切换为只读?

                  O(N) or O(1), i.e. does the list get copied or is something somewhere internally switched from writable to read-only?

                  非常感谢!

                  推荐答案

                  这是一个 O(N) 操作,tuple(list) 只是简单地将对象从列表中复制到元组中.所以,您仍然可以修改内部对象(如果它们是可变的),但您不能向元组添加新项目.

                  It is an O(N) operation, tuple(list) simply copies the objects from the list to the tuple. SO, you can still modify the internal objects(if they are mutable) but you can't add new items to the tuple.

                  复制列表需要 O(N) 时间.

                  >>> tup = ([1, 2, 3],4,5 ,6)
                  >>> [id(x) for x in tup]
                  [167320364, 161878716, 161878704, 161878692]
                  >>> lis = list(tup)
                  

                  内部对象仍然引用相同的对象

                  Internal object still refer to the same objects

                  >>> [id(x) for x in lis]
                  [167320364, 161878716, 161878704, 161878692]
                  

                  但是外部容器现在是不同的对象.因此,修改外部对象不会影响其他对象.

                  But outer containers are now different objects. So, modifying the outer objects won't affect others.

                  >>> tup is lis
                  False
                  >>> lis.append(10)
                  >>> lis, tup
                  ([[1, 2, 3], 4, 5, 6, 10], ([1, 2, 3], 4, 5, 6)) #10 not added in tup
                  

                  修改一个可变的内部对象会影响两个容器:

                  Modifying a mutable internal object will affect both containers:

                  >>> tup[0].append(100)
                  >>> tup[0], lis[0]
                  ([1, 2, 3, 100], [1, 2, 3, 100])
                  

                  时间比较表明列表复制和元组创建花费的时间几乎相同,但由于创建具有新属性的新对象有开销,因此创建元组稍微昂贵.

                  Timing comparison suggest list copying and tuple creation take almost equal time, but as creating a new object with new properties has it's overhead so tuple creation is slightly expensive.

                  >>> lis = range(100)
                  >>> %timeit lis[:]
                  1000000 loops, best of 3: 1.22 us per loop
                  >>> %timeit tuple(lis)
                  1000000 loops, best of 3: 1.7 us per loop
                  >>> lis = range(10**5)
                  >>> %timeit lis[:]
                  100 loops, best of 3: 2.66 ms per loop
                  >>> %timeit tuple(lis)
                  100 loops, best of 3: 2.77 ms per loop
                  

                  这篇关于python中将列表转换为元组的时间复杂度,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:列表 += 元组与列表 = 列表 + 元组 下一篇:Python:元组/字典作为键、选择、排序

                  相关文章

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

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

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

                  2. <legend id='ad5pz'><style id='ad5pz'><dir id='ad5pz'><q id='ad5pz'></q></dir></style></legend>

                    1. <tfoot id='ad5pz'></tfoot>