named tuple - 네임드듀플
namedtuple은 tuple과 비슷한 성능과 특성을 갖는다 tuple 항목을 index 위치뿐 아니라 name으로도 참조할 수 있다 >>> from collections import namedtuple >>> >>> Animal = namedtuple('Animal', 'name species sex') >>> # Animal = namedtuple('Animal', ['name','species','sex']) ... # Animal = namedtuple('Animal', ('name','species','sex')) ... a = Animal('pororo', 'penguin', 'male') >>> a Animal(name='pororo', species='penguin', sex='male'..
2020. 9. 11.
Array(list)의 원소 개수 카운팅
collections 의 defaultdict로 배열의 엘러먼트 카운트를 하나의 함수도 대신하기 collections.Counter를 사용하면된다 from collections import defaultdict s = ['a', 'b', 'c', 'b', 'a', 'b', 'c'] dd = defaultdict(int) for k in s: dd[k]+=1 print(dd) defaultdict(, {'a': 2, 'b': 3, 'c': 2}) >>> from collections import Counter >>> d = Counter(s) >>> print(d) Counter({'b': 3, 'a': 2, 'c': 2})
2020. 6. 27.