Time complexity of casting lists to tuples in python and vice versa

假如想象 提交于 2019-12-12 11:22:26

问题


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) or O(1), i.e. does the list get copied or is something somewhere internally switched from writable to read-only?

Thanks a lot!


回答1:


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.

Copying a list takes O(N) time.

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



回答2:


As far as I understand, there's no bit to switch, as the list (mutable) object is completely different than the tuple (immutable) object. They have different methods etc.

One experiment you might do is this:

>>> a = [1,2,3,4,5]
>>> a = (1,2,3,4,5)
>>> a
(1, 2, 3, 4, 5)
>>> b = list(a)
>>> b
[1, 2, 3, 4, 5]
>>> b[2] = 'a'
>>> b
[1, 2, 'a', 4, 5]
>>> a
(1, 2, 3, 4, 5)

See, if they were referencing the exact place in the memory then a should have changed as well.

That's for my understanding.



来源:https://stackoverflow.com/questions/18726969/time-complexity-of-casting-lists-to-tuples-in-python-and-vice-versa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!