collections의 defaultdict 객체

정은경·2020년 6월 24일
0

defaultdict 객체



defaultdict 객체

dir(defaultdict)

>>> from collections import defaultdict
>>>
>>> dir(defaultdict)
['__class__', '__contains__', '__copy__', '__delattr__', '__delitem__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', 
'__iter__', '__le__', '__len__', '__lt__', '__missing__', '__ne__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', 
'__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 
'default_factory', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 
'setdefault', 'update', 'values']
>>>
>>>

d = defaultdict(list)

>>> list
<class 'list'>
>>> d = defaultdict(list)
>>>
>>> dir(d)
['__class__', '__contains__', '__copy__', '__delattr__', '__delitem__', 
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', 
'__iter__', '__le__', '__len__', '__lt__', '__missing__', '__ne__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', 
'__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 
'default_factory', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 
'setdefault', 'update', 'values']
>>>
>>> dir(defaultdict)
['__class__', '__contains__', '__copy__', '__delattr__', '__delitem__', 
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', 
'__iter__', '__le__', '__len__', '__lt__', '__missing__', '__ne__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', 
'__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 
'default_factory', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 
'setdefault', 'update', 'values']
>>>
>>>

soretd(d.items())

>>> d = defaultdict(list)
>>> s = [('d',1), ('c',2), ('b',3), ('a',4)]
>>> s
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
>>>
>>> d
defaultdict(<class 'list'>, {})
>>>
>>> for k,v in s:
...     print(f"k: {k}, v: {v}")
...
k: d, v: 1
k: c, v: 2
k: b, v: 3
k: a, v: 4
>>>
>>>
>>> for k,v in s:
...     d[k].append(v)
...
>>>
>>> d
defaultdict(<class 'list'>, {'d': [1], 'c': [2], 'b': [3], 'a': [4]})
>>>
>>> d.items()
dict_items([('d', [1]), ('c', [2]), ('b', [3]), ('a', [4])])
>>>
>>> d.keys()
dict_keys(['d', 'c', 'b', 'a'])
>>>
>>> sorted(d.items())
[('a', [4]), ('b', [3]), ('c', [2]), ('d', [1])]
>>>
>>> d
defaultdict(<class 'list'>, {'d': [1], 'c': [2], 'b': [3], 'a': [4]})
>>>

defaultdict(list)와 defaultdict(int)

>>> d = defaultdict(list)
>>> d
defaultdict(<class 'list'>, {})
>>>
>>> dir(d)
['__class__', '__contains__', '__copy__', '__delattr__', '__delitem__', 
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', 
'__iter__', '__le__', '__len__', '__lt__', '__missing__', '__ne__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', 
'__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 
'default_factory', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 
'setdefault', 'update', 'values']
>>>
>>>
>>> d = defaultdict(int)
>>> d
defaultdict(<class 'int'>, {})
>>>
>>> dir(d)
['__class__', '__contains__', '__copy__', '__delattr__', '__delitem__', 
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', 
'__iter__', '__le__', '__len__', '__lt__', '__missing__', '__ne__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', 
'__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 
'default_factory', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 
'setdefault', 'update', 'values']
>>>

defaultdict(int)

>>> d = defaultdict(int)
>>> d
defaultdict(<class 'int'>, {})
>>>
>>> s = 'mississippi'
>>> for k,v in s:
...     print(f"k: {k}, v: {v}")
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 2, got 1)
>>>
>>> for k in s:
...     print(f"k: {k}")
...
k: m
k: i
k: s
k: s
k: i
k: s
k: s
k: i
k: p
k: p
k: i
>>>
>>>
>>> for k in s:
...     d[k] += 1
...
>>> d
defaultdict(<class 'int'>, {'m': 1, 'i': 4, 's': 4, 'p': 2})
>>>
>>> d.items()
dict_items([('m', 1), ('i', 4), ('s', 4), ('p', 2)])
>>>
>>> sorted(d.items())
[('i', 4), ('m', 1), ('p', 2), ('s', 4)]
>>>
>>> d
defaultdict(<class 'int'>, {'m': 1, 'i': 4, 's': 4, 'p': 2})
>>>

참고: 리스트의 특성

>>> s = 'mississippi'
>>>
>>> for k,v in s:
...     print(f"k: {k}, v: {v}")
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 2, got 1)
>>>
>>> for k,v in enumerate(s):
...     print(f"k: {k}, v: {v}")
...
k: 0, v: m
k: 1, v: i
k: 2, v: s
k: 3, v: s
k: 4, v: i
k: 5, v: s
k: 6, v: s
k: 7, v: i
k: 8, v: p
k: 9, v: p
k: 10, v: i
>>>
>>> for k in s:
...     print(f"k: {k}")
...
k: m
k: i
k: s
k: s
k: i
k: s
k: s
k: i
k: p
k: p
k: i
>>>

d = defaultdict(set)

>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
>>> s
[('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
>>>
>>> for k, v in s:
...     d[k].add(v)
...     print(f"d: {d}, k: {k}, v: {v}")
...
d: defaultdict(<class 'set'>, {'red': {1}}), k: red, v: 1
d: defaultdict(<class 'set'>, {'red': {1}, 'blue': {2}}), k: blue, v: 2
d: defaultdict(<class 'set'>, {'red': {1, 3}, 'blue': {2}}), k: red, v: 3
d: defaultdict(<class 'set'>, {'red': {1, 3}, 'blue': {2, 4}}), k: blue, v: 4
d: defaultdict(<class 'set'>, {'red': {1, 3}, 'blue': {2, 4}}), k: red, v: 1
d: defaultdict(<class 'set'>, {'red': {1, 3}, 'blue': {2, 4}}), k: blue, v: 4
>>>
>>> d
defaultdict(<class 'set'>, {'red': {1, 3}, 'blue': {2, 4}})
>>>
>>> d.items()
dict_items([('red', {1, 3}), ('blue', {2, 4})])
>>>
>>> sorted(d.items())
[('blue', {2, 4}), ('red', {1, 3})]
>>>
>>> d
defaultdict(<class 'set'>, {'red': {1, 3}, 'blue': {2, 4}})
>>>

Reference

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글