developer tip

사전을 튜플로 풀 수있는 이유는 무엇입니까?

copycodes 2020. 12. 1. 08:20
반응형

사전을 튜플로 풀 수있는 이유는 무엇입니까?


오늘 저는 예외를 던지지 않는 한 문장을 보았습니다. 누구든지 그 뒤에있는 이론을 설명 할 수 있습니까?

>>> x, y = {'a': 2, 'b': 5}
>>> x
'a'
>>> y
'b'

파이썬에서는 모든 이터 러블 을 풀 수 있습니다 1 :

>>> x,y,z = [1, 2, 3]  # A list
>>> x,y,z
(1, 2, 3)
>>> x,y,z = 1, 2, 3  # A tuple
>>> x,y,z
(1, 2, 3)
>>> x,y,z = {1:'a', 2:'b', 3:'c'}  # A dictionary
>>> x,y,z
(1, 2, 3)
>>> x,y,z = (a for a in (1, 2, 3))  # A generator
>>> x,y,z
(1, 2, 3)
>>>

또한 딕셔너리를 반복하면 키만 반환되기 때문에 :

>>> for i in {1:'a', 2:'b', 3:'c'}:
...     print i
...
1
2
3
>>>

딕셔너리를 풀면 (반복하는) 마찬가지로 키만 풀립니다.


1 사실, 나는 모든 반복자가 압축 해제 할 수 있다고한다 긴만큼 압축 풀기로로 이름이 반복자의 길이를 동일 :

>>> a,b,c = [1, 2, 3]  # Number of names == len(iterable)
>>>
>>> a,b = [1, 2, 3]  # Too few names
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
>>>
>>> a,b,c,d = [1, 2, 3]  # Too many names
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 3 values to unpack
>>>

그러나 이것은 Python 2.x의 경우에만 해당됩니다. Python 3.x에서는 iterable unpacking확장 하여 (유한) 크기의 iterable을 필요한 이름으로 만 압축 해제 할 수 있습니다.

>>> # Python 3.x interpreter
...
>>> a, *b, c = [1, 2, 3, 4]
>>> a, b, c
(1, [2, 3], 4)
>>>
>>> a, *b = [1, 2, 3, 4]
>>> a, b
(1, [2, 3, 4])
>>>
>>> *a, b, c = [1, 2, 3, 4]
>>> a, b, c
([1, 2], 3, 4)
>>>

Iterating a dict iterates over the keys. Since your dict literal has exactly two keys, you can unpack it into a 2-tuple.

This is probably not a good practice in general, since (before python 3.7, or possibly earlier in some other implementations) dicts are unordered and x == 'b' and y == 'a' would be a perfectly legal outcome of that code.


when you iterate over a dictionary, you get its keys

data = {'a': 2, 'b': 5}
for key in data:
    print key

Unpacking is nothing else than iterating over the object and put the elements in the given variables:

keys = tuple(data) # gives ('a', 'b')
x, y = ('a', 'b')

No rocket science behind it. dict is an iterable, which return the keys in each iteration. tuple() can receive any iterable as argument (as long as they are finite), so:

>>>tuple({'a': 2, 'b': 5})
('a','b')

Seeing this, is easy to infer that unpacking will work as shown. Moreover, any finite iterable can be unpacked:

>>> i = iter(range(3))
>>> a,b,c = i
>>> a,b,c
(0, 1, 2)

When in iterable context, dicts are treated as an (unordered) collection of keys, which is what you get when you do list(some_dict), which is the same as calling keys() on the dict:

>>> d = {'a': 3, 'b': 5}
>>> list(d)
['a', 'b']
>>> d.keys()
['a', 'b']

However, you can also do more.

You can unpack both a dict's both keys and values if you turn it into a list of pairs first:

>>> d = {'a': 3, 'b': 5}
>>> d_pairs = d.items()
>>> print d_pairs
[('a', 3), ('b', 5)]
>>> ((k1, v1), (k2, v2)) = d_pairs
>>> print k1, v1, k2, v2
a 3 b 5

or if you just want the pairs

>>> p1, p2 = d_pairs
>>> print p1, p2
('a', 3) ('b', 5)

or, say, just the keys:

>>> ((k1, _), (k2, _)) = d_pairs
>>> print k1, k2
a b

etc.

But of course since dictionaries — and I mean in general, not only in Python — contain their items in an un-ordered manner, items() (in Python) will also return them in a seemingly arbitrary order, and thus there is no way to know which key will be stored in which variable:

>>> ((k1, v1), (k2, v2)) = {'bar': 3, 'foo': 5}.items()
>>> print k1, v1, k2, v2
foo 5 bar 3

As you see, the order of the pairs returned by items() was reversed in comparison to their definition order.

참고URL : https://stackoverflow.com/questions/23268615/why-can-a-dictionary-be-unpacked-as-a-tuple

반응형