ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • <PYTHON>[collections]
    Flower in my dev/Python 2015. 7. 10. 11:21

    [collections]

    - 내부 함수는 아래만 활용해 본다.

    Counter()

    deque()

    defaultdict()

    OrderedDict()

    namedtuple()

     

     

    1. Counter()

     

    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
    26
    27
    28
    import re
    import collections
     
    cnt = collections.Counter()
     
    words = ['flower','wing','star','flower','star','flower']
     
    for w in words:
        cnt[w] += 1
     
    print cnt
     
    print list(cnt.elements())
     
    chk = re.findall(r'\w+'open('test.txt').read().lower())
     
    print collections.Counter(chk).most_common(10)
    print collections.Counter(chk).most_common(5)
     
    nCnt01 = collections.Counter(a=5, b=3, c=1, d=-1)
    nCnt02 = collections.Counter(a=2, b=4, c=3, d=1)
    nCnt03 = collections.Counter(a=5, b=3, c=1, d=-1)
     
    print nCnt01.subtract(nCnt02)
    print nCnt03 + nCnt02
    print nCnt03 - nCnt02
    print nCnt03 & nCnt02
    print nCnt03 | nCnt02
    cs

     

     

     

     

    2. deque()

     

    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
    26
    27
    28
    import collections
     
    = collections.deque('flower')
     
    for elem in d:
        print elem.upper()
     
    d.append('a')
    d.appendleft('z')
    print d
    print d.pop()
    print d.popleft()
    print list(d)
    print d[0]
    print d[-1]
    print list(reversed(d))
    print 'w' in d
    d .extend(' teacher')
    print d
    d.rotate(1)
    print d
    d.rotate(-1)
    print d
    print collections.deque(reversed(d))
    d.clear()
    print d
    d.extendleft('wing')
    print d
    cs

     

     

     

     

    3. defaultdict()

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    import collections
     
    = [('flower',3),('wing',1),('star',2),('sky',5)]
    = collections.defaultdict(list)
     
    for k, v in s:
        d[k].append(v)
     
    print d.items()
     
    = 'flowerflowerfflowwwwerrr'
    = collections.defaultdict(int)
    for k in s:
        d[k] += 1
     
    print d.items()
     
    = [('flower'1),('wing',2),('flower',3),('wing',5),('wing',5)]
    = collections.defaultdict(set)
    for k, v in s:
        d[k].add(v)
     
    print d.items()
    cs

     

     

     

     

    4. OrderedDict()

     

    1
    2
    3
    4
    5
    6
    7
    from collections import OrderedDict
     
    = {'flower'2'wing'1'star'4'sky'3}
     
    print OrderedDict(sorted(d.items(), key=lambda t: t[0]))
    print OrderedDict(sorted(d.items(), key=lambda t: t[1]))
    print OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
    cs

     

     

     


    5. namedtuple()


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    from collections import namedtuple
     
    info = namedtuple('info', ['name''sex''area'])
     
    name = 'wing flower sky snow'.split()
    sex = 'mail femail'.split()
    area = 'Korea USA'.split()
     
    INFO = [info(n, s, a) for n in name for s in sex for a in area]
     
    print(info)
    for named in INFO:
        print(named)
    print(type(INFO))
    print(type(INFO[0]))
    cs



    'Flower in my dev > Python' 카테고리의 다른 글

    <PYTHON>[binascii]  (0) 2015.07.10
    <PYTHON>[dpkt]  (0) 2015.07.10
    <PYTHON>[.append & .extend]  (0) 2015.07.09
    <PYTHON>[ip 변환]  (0) 2015.07.09
    <PYTHON>[locale]  (0) 2015.07.09

    댓글

Designed by Tistory.