라벨이 collections인 게시물 표시

파이썬[Python]: collections - data 변수 (UserString)

collections 모듈 - data 변수(variable) /// 설명 UserString 클래스의 값을 확인합니다. ※ 형식 UserString.data reference https://docs.python.org/3/library/collections.html#module-collections /// 예제 1 2 3 4 5 6 7 8 9 import  collections   test_str  =   'This is a string' print (test_str)   # This is a string   test_ostr  =  collections.UserString(test_str) print (test_ostr.data)   # This is a string print (test_ostr.removeprefix( 'This ' ))   # is a string   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: collections - data 변수 (UserList)

collections 모듈 - data 변수(variable) /// 설명 UserList 클래스의 값을 확인합니다. ※ 형식 UserList.data reference https://docs.python.org/3/library/collections.html#module-collections /// 예제 1 2 3 4 5 6 7 8 9 10 import  collections   test_list  =  [ 6 ,  2 ,  3 ,  4 ,  5 ] print (test_list)   # [6, 2, 3, 4, 5]   test_olist  =  collections.UserList(test_list) print (test_olist.data)   # [6, 2, 3, 4, 5] test_olist.clear() print (test_olist.data)   # []   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: collections - data 변수 (UserDict)

collections 모듈 - data 변수(variable) /// 설명 UserDict 클래스의 값을 확인합니다. ※ 형식 UserDict.data reference https://docs.python.org/3/library/collections.html#module-collections /// 예제 1 2 3 4 5 6 7 8 9 10 import  collections   test_dict  =  { 'a' :  6 ,  'b' :  2 ,  'd' :  3 ,  'g' :  4 ,  'c' :  5 } print (test_dict)   # {'a': 6, 'b': 2, 'd': 3, 'g': 4, 'c': 5}   test_odict  =  collections.UserDict(test_dict) print (test_odict.data)   # {'a': 6, 'b': 2, 'd': 3, 'g': 4, 'c': 5} test_odict.clear() print (test_odict.data)   # {}   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: collections - _field_defaults 변수

collections 모듈 - _field_defaults 변수(variable) /// 설명 튜플에 주어지는 기본값을 표현합니다.(주어진 값은 변하지 않습니다.) ※ 형식 namedtuple._field_defaults reference https://docs.python.org/3/library/collections.html#module-collections /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import  collections   Test1  =  collections.namedtuple( 'Test1' , [ 'a' ,  'b' ,  'c' ], defaults = ( 1 ,  2 ))   test_nt  =  Test1(a = 'a' ) print (test_nt)   # Test1(a='a', b=1, c=2) print (Test1._field_defaults)   # {'b': 1, 'c': 2}   Test2  =  collections.namedtuple( 'Test2' , [ 'a' ,  'b' ,  'c' ], defaults = ( 1 ,  2 ,  3 ))   test_nt  =  Test2() print (test_nt)   # Test2(a=1, b=2, c=3) print (test_nt._field_defaults)   # {'a': 1, 'b': 2, 'c': 3} test_nt  =  Test2(a = 'a' ) print (test_nt)   # Test2(a='a', b=2, c=3) print (Test2._field_defaults)   # {'a': 1, 'b': 2, 

파이썬[Python]: collections - _fields 변수

collections 모듈 - _fields 변수(variable) /// 설명 필드(field) 이름을 표현하는 튜플이며, 필드를 합쳐서 새로운 named 튜플을 생성하는데 유용합니다. ※ 형식 namedtuple._fields reference https://docs.python.org/3/library/collections.html#module-collections /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import  collections   Test1  =  collections.namedtuple( 'Test1' , [ 'a' ,  'b' ,  'c' ])   test_nt  =  Test1(a = 1 , b = 2 , c = 3 ) print (test_nt)   # Test1(a=1, b=2, c=3) print (test_nt._fields)   # ('a', 'b', 'c')   Test2  =  collections.namedtuple( 'Test2' , [ 'd' ,  'e' ])   test_nt  =  Test2(d = 5 , e = 6 ) print (test_nt)   # Test2(d=5, e=6) print (test_nt._fields)   # ('d', 'e')   Test3  =  collections.namedtuple( 'Test3' , Test1._fields  +  Test2._fields) test_nt  =  Test3(a = 3 , b = 4 , c = 5 , d = 6 , e = 7 ) print (test_nt)   # Test3(a=3, b=4, c=5, d=6, e=7) print (test_nt._fields)   # ('a&#

파이썬[Python]: collections - default_factory 변수

collections 모듈 - default_factory 변수(variable) /// 설명 __missing__() 메서드에 의해 사용되어집니다. 기본값은 None입니다. ※ 형식 defaultdict.default_factory reference https://docs.python.org/3/library/collections.html#module-collections /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import  collections   test_list  =  [( 'a' ,  1 ), ( 'b' ,  2 ), ( 'a' ,  3 ), ( 'b' ,  4 ), ( 'c' ,  5 )] print (test_list)   # [('a', 1), ('b', 2), ('a', 3), ('b', 4), ('c', 5)]   test_ddict  =  collections.defaultdict(lambda:  'None' , test_list)   print (test_ddict)   # defaultdict(<function <lambda> at 0x000001407028BD30>, {'a': 3, 'b': 4, 'c': 5})   print (test_ddict.default_factory)   # <function <lambda> at 0x000001407028BD30> print (test_ddict[ 'q' ])   # None     def  d_factory():      return   'None'     test_ddict  =  collections.def

파이썬[Python]: collections - maxlen 변수

collections 모듈 - maxlen 변수(variable) /// 설명 deque의 최대 크기를 반환합니다.(설정되어 있지 않으면 None을 반환합니다.) ※ 형식 deque.maxlen reference https://docs.python.org/3/library/collections.html#module-collections /// 예제 1 2 3 4 5 6 7 8 9 10 import  collections   test_list  =  [ 'f' ,  'e' ,  'a' ,  'a' ,  'b' ,  'c' ,  'd' ,  'd' ,  'd' ]   # deque: a double-ended queue, pronounced deck test_dq  =  collections.deque(test_list,  20 ) print (test_dq)   # deque(['f', 'e', 'a', 'a', 'b', 'c', 'd', 'd', 'd'])   print (test_dq.maxlen)   # 20   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: collections - parents 변수

collections 모듈 - parents 변수(variable) /// 설명 처음 하나를 제외하고 모든 맵들을 포함하는 새로운 Chainmap을 반환합니다. ※ 형식 chainmap.parents reference https://docs.python.org/3/library/collections.html#module-collections /// 예제 1 2 3 4 5 6 7 8 9 10 11 import  collections   test_dict1  =  { 'a' :  1 ,  'b' :  2 ,  'c' :  3 } test_dict2  =  { 'd' :  4 ,  'b' :  5 ,  'e' :  6 }   test_cm  =  collections.ChainMap(test_dict1, test_dict2)   print (test_cm.parents)   # ChainMap({'d': 4, 'b': 5, 'e': 6})   print (collections.ChainMap( * test_cm.maps[ 1 :]))   # ChainMap({'d': 4, 'b': 5, 'e': 6})   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: collections - UserString 클래스

collections 모듈 - UserString 클래스(class) /// 설명 스트링 객체의 래퍼 처럼 작동하는 클래스입니다. 스트링 객체의 내용을 속성(data attribute)으로서 접근할 수 있기 때문에 사용되어 집니다. ※ 형식 class collections.UserString(initialdata) /// 예제 1 2 3 4 5 6 7 8 9 import  collections   test_str  =   'This is a string' print (test_str)   # This is a string   test_ostr  =  collections.UserString(test_str) print (test_ostr.data)   # This is a string print (test_ostr.removeprefix( 'This ' ))   # is a string   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: collections - UserList 클래스

collections 모듈 - UserList 클래스(class) /// 설명 리스트 객체의 래퍼 처럼 작동하는 클래스입니다. 리스트 객체의 내용을 속성(data attribute)으로서 접근할 수 있기 때문에 사용되어 집니다. ※ 형식 class collections.UserList(initialdata) /// 예제 1 2 3 4 5 6 7 8 9 10 import  collections   test_list  =  [ 6 ,  2 ,  3 ,  4 ,  5 ] print (test_list)   # [6, 2, 3, 4, 5]   test_olist  =  collections.UserList(test_list) print (test_olist.data)   # [6, 2, 3, 4, 5] test_olist.clear() print (test_olist.data)   # []   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: collections - UserDict 클래스

collections 모듈 - UserDict 클래스(class) /// 설명 딕셔너리 객체의 래퍼 처럼 작동하는 클래스입니다. 딕셔너리 객체의 내용을 속성(data attribute)으로서 접근할 수 있기 때문에 사용되어 집니다. ※ 형식 class collections.UserDict(initialdata) /// 예제 1 2 3 4 5 6 7 8 9 10 import  collections   test_dict  =  { 'a' :  6 ,  'b' :  2 ,  'd' :  3 ,  'g' :  4 ,  'c' :  5 } print (test_dict)   # {'a': 6, 'b': 2, 'd': 3, 'g': 4, 'c': 5}   test_odict  =  collections.UserDict(test_dict) print (test_odict.data)   # {'a': 6, 'b': 2, 'd': 3, 'g': 4, 'c': 5} test_odict.clear() print (test_odict.data)    # {}   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: collections - OrderedDict 클래스

collections 모듈 - OrderedDict 클래스(class) /// 설명 딕셔너리 순서를 재 정렬하기 위한 특별한 메서드들을 제공하는 dict subclass의 인스턴스를 반환합니다. ※ 형식 class collections.OrderedDict(*maps) /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 import  collections   test_dict  =  { 'a' :  6 ,  'b' :  2 ,  'd' :  3 ,  'g' :  4 ,  'c' :  5 ,  'r' :  3 ,  'h' :  1 } print (test_dict)   # {'a': 6, 'b': 2, 'd': 3, 'g': 4, 'c': 5, 'r': 3, 'h': 1}   test_odict  =  collections.OrderedDict(test_dict) print (test_odict) # OrderedDict([('a', 6), ('b', 2), ('d', 3), ('g', 4), ('c', 5), ('r', 3), ('h', 1)])   test_r  =  reversed(test_odict.items()) print (list(test_r))   # [('h', 1), ('r', 3), ('c', 5), ('g', 4), ('d', 3), ('b', 2), ('a', 6)]   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파

파이썬[Python]: collections - namedtuple 클래스

collections 모듈 - namedtuple 클래스(class) /// 설명 typename이름으로 되어진 새로운 튜플의 subclass를 반환합니다. 이 subclass는 튜플 객체를 생성하는데 사용되어 집니다.(속성(attribute)과 색인(index)으로 접근 가능합니다.) rename 인자는 fieldname에 유효하지 않은 값이 들어오면 자동으로 변환하여줍니다. default 인자는 fieldname에 값을 전달하여 줍니다.(오른쪽 파라미터부터 접근합니다.) module 인자는 생성된 튜플의 __module__ 속성을 이 값으로 만들어 줍니다. ※ 형식 class collections.namedtuple(typename, field_names, *, rename=False, defaults=None, module=None) /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import  collections   # Test = collections.namedtuple('Test', 'a b c') # Test = collections.namedtuple('Test', 'a, b, c') Test  =  collections.namedtuple( 'Test' , [ 'a' ,  'b' ,  'c' ])   print (type(Test))   # <class 'type'> print (Test)   # <class '__main__.Test'>   test_nt  =  Test(a = 1 , b = 2 , c = 3 ) print (test_nt)   # Test(a=1, b=2, c=3) print (test_nt[ 0 ], test_nt[ 1 ], test_nt[ 2 ])

파이썬[Python]: collections - defaultdict 클래스

collections 모듈 - defaultdict 클래스(class) /// 설명 dict 클래스의 서브클래스로 새로운 딕셔너리 객체를 반환합니다. default_factory인자를 제외하고는 dict 생성자의 인자와 같습니다. default_factory인자(기본값은 None)는 키값이 존재하지 않을 경우 표현할 수 있는 값입니다. ※ 형식 class collections.defaultdict(default_factory=None, /[, ...]) /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import  collections   test_ddict  =  collections.defaultdict() print (test_ddict)   # defaultdict(None, {}) print (type(test_ddict))   # <class 'collections.defaultdict'>   test_list  =  [( 'a' ,  1 ), ( 'b' ,  2 ), ( 'a' ,  3 ), ( 'b' ,  4 ), ( 'c' ,  5 )] print (test_list)   # [('a', 1), ('b', 2), ('a', 3), ('b', 4), ('c', 5)]   test_ddict  =  collections.defaultdict(list) for  k, v  in  test_list:     test_ddict[k].append(v)   print (test_ddict)   # defaultdict(<class 'list'>, {'a': [1, 3], 'b': [2, 4], 'c': [5]})   # diction

파이썬[Python]: collections - deque 클래스

collections 모듈 - deque 클래스(class) /// 설명 반복 가능한 객체의 요소들을 왼쪽에서 오른쪽으로 초기화하여 deque 객체를 반환합니다. 인자 maxlen은 deque 의 최대길이를 지정합니다. maxlen이 지정되지 않으면 deque는 arbitary length가 될 수 있습니다.(arbitary length: infinite 인 듯합니다.) ※ 형식 class collections.deque(iterable) class collections.deque(iterable, maxlen) /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import  collections   test_dq  =  collections.deque() print (test_dq)   # deque([]) print (type(test_dq))   # <class 'collections.Counter'>   test_list  =  [ 'f' ,  'e' ,  'a' ,  'a' ,  'b' ,  'c' ,  'd' ,  'd' ,  'd' ]   print (test_list)   # ['f', 'e', 'a', 'a', 'b', 'c', 'd', 'd', 'd']   # deque: a double-ended queue, pronounced deck test_dq  =  collections.deque(test_list) print (test_dq)   # deque(['f', 'e', 'a', 'a', 'b', 'c', 'd',

파이썬[Python]: collections - Counter 클래스

collections 모듈 - Counter 클래스(class) /// 설명 이터러블 안에 포함되어 있는 각 객체의 개수를 표현하며, {객체: 객체의 수}의 형태로 저장이 됩니다. ※ 형식 class collections.Counter(iterable-or-mapping) /// 예제 1 2 3 4 5 6 7 8 9 10 import  collections   test_list  =  [ 'a' ,  'a' ,  'b' ,  'c' ,  'd' ,  'd' ,  'd' ]   print (test_list)   # ['a', 'a', 'b', 'c', 'd', 'd', 'd']   test_c  =  collections.Counter(test_list) print (type(test_c))   # <class 'collections.Counter'> print (test_c)   # Counter({'d': 3, 'a': 2, 'b': 1, 'c': 1})   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: collections - maps 변수

collections 모듈 - maps 변수(variable) /// 설명 ChinMap에 형성되어 있는 매핑들의 리스트를 보여줍니다.(순서: first-searched to last-searched) ※ 형식 chainmap.maps reference https://docs.python.org/3/library/collections.html#module-collections /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import  collections   test_cm  =  collections.ChainMap()   print (test_cm)   # ChainMap({}) print (test_cm.maps)   # [{}]   test_dict1  =  { 'a' :  1 ,  'b' :  2 ,  'c' :  3 } test_dict2  =  { 'd' :  4 ,  'b' :  5 ,  'e' :  6 }   test_cm  =  collections.ChainMap(test_dict1, test_dict2)   print (test_cm)   # ChainMap({'a': 1, 'b': 2, 'c': 3}, {'d': 4, 'b': 5, 'e': 6}) print (test_cm.maps)   # [{'a': 1, 'b': 2, 'c': 3}, {'d': 4, 'b': 5, 'e': 6}]   # the iteration order is determined by scanning the mappings last to first. print (list(test_cm))   # ['d', 'b'

파이썬[Python]: collections - ChainMap 클래스

collections 모듈 - ChainMap 클래스(class) /// 설명 여러 딕셔너리들이나 다른 매핑들을 함께 하나로 만들어주는 클래스입니다. 맵(map)이 주어지지 않는다면 빈 딕셔너리 하나가 생성됩니다. 이 맵핑들은 리스트에 저장됩니다. ※ 형식 class collections.ChainMap(*maps) /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import  collections   test_cm  =  collections.ChainMap()   print (test_cm)   # ChainMap({}) print (type(test_cm))   # <class 'collections.ChainMap'>   test_dict1  =  { 'a' :  1 ,  'b' :  2 ,  'c' :  3 } test_dict2  =  { 'd' :  4 ,  'b' :  5 ,  'e' :  6 }   test_cm  =  collections.ChainMap(test_dict1, test_dict2)   print (test_cm)   # ChainMap({'a': 1, 'b': 2, 'c': 3}, {'d': 4, 'b': 5, 'e': 6})   # the iteration order is determined by scanning the mappings last to first. print (list(test_cm))   # ['d', 'b', 'e', 'a', 'c']   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파