파이썬에서 제공하는 자료구조

1. Stack

  • Last In First Out (LIFO)
a = [1, 2, 3, 4, 5]
a.append(10)
print(a)
a.append(20)
print(a)
print(a.pop())
print(a)
print(a.pop())
word = input("Input a word:")
word_list = list(word)
print(word_list)

result = []
for _ in range(len(word_list)):
    result.append(word_list.pop())

print(result)
print(word[::-1])

2. Queue

  • First In First Out(FIFO)
a = [1, 2, 3, 4, 5]
a.append(10)
print(a)
a.append(20)
print(a)
print(a.pop(0))
print(a)
print(a.pop(0))

3. Tuple

  • 리스트와 비슷하지만, 데이터 변경이 안됨
t = (1, 2, 3)
print(t+t, t*2)
print(len(t))
#t[2] = 4  # 데이터 변경이 안되서 에러가 발생함
t2 = (1,) # 요소가 하나인 튜플을 만들때는 ,를 붙여줌
print(t2)

4. Set

  • 데이터 중복을 허용하지 않음
  • 순서가 없음
s = set([1, 2, 3, 1, 2, 3])
print(s)

s.add(1)
print(s)
s.remove(1) # 없는 항목을 제거하면 error
print(s)
s.update([1, 4, 5, 6, 7])
print(s)
s.discard(3) # 없는 항목을 제거하면 pass
print(s)
s.clear()
print(s)
s1 = set([1, 2, 3, 4, 5])
s2 = set([3, 4, 5, 6, 7])

print(s1.union(s2))
print(s1 | s2)

print(s1.intersection(s2))
print(s1 & s2)

print(s1.difference(s2))
print(s1 - s2)

5. Dictionary

  • Key-Value 형태로 저장
student_info = {20190001: "Tom", 20190002: "Jane", 20190003 : "Mike", 20190004: "Jessica"}
print(student_info[20190002])

student_info[20190002] = "Kate"
print(student_info)

student_info[20190005] = 'David'
print(student_info)

country_code = {'USA': 1, 'Korea': 82, 'China': 86, 'Malaysia': 60}
print(country_code)

print(country_code.keys())

country_code['German'] = 49
print(country_code)

print(country_code.values())
print(country_code.items()) # 튜플 형태의 리스트로 만들어줌
print()

for k, v in country_code.items():
    print('Key:', k)
    print('Value:', v)

print('Korea' in country_code.keys())
print(85 in country_code.values())

6. Collections 모듈

  • 위 자료구조를 지원하는 파이썬 내장 모듈
  • 기본 자료구조를 확장하여 미리 제작하고, 파이썬 모듈로 제공
  • deque, OrderedDict, defaultdict, Counter, namedtuple 등

6.1 deque

from collections import deque

deque_list = deque()
for i in range(5):
    deque_list.append(i)

print(deque_list)

print(deque_list.pop())
print(deque_list.pop())
print(deque_list)

deque_list.appendleft(5)
deque_list.appendleft(6)
print(deque_list)

print(deque_list.popleft())
print(deque_list.popleft())
print(deque_list)
from collections import deque


deque_list = deque()
for i in range(5):
    deque_list.appendleft(i)

print(deque_list)

deque_list.rotate(2)  # 순서를 오른쪽으로 2번 회전
print(deque_list)

deque_list.rotate(-2)  # 순서를 왼쪽으로 2번 회전
print(deque_list)

deque_list.extend([5, 6, 7])  # 오른쪽에 붙임 순서 대로
print(deque_list)

deque_list.extendleft([9, 10])  # 왼쪽에 붙임 역순으로
print(deque_list)

6.2 OrderedDict

  • 순서를 보장하는 dictionary
from collections import OrderedDict

d = dict()
d['x'] = 100
d['y'] = 200
d['z'] = 300
d['a'] = 400
print(d)

od = OrderedDict(sorted(d.items(), key=lambda x:x[0]))

print()
print(od)

od_list = sorted(od.items(), key=lambda x: x[1])
print()
print(od_list)

6.3 defaultdict

from collections import defaultdict


d = defaultdict(lambda: 100)
print(d['first'])

d2 = defaultdict(int)
print(d2['a'])

s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d3 = defaultdict(list)

for k, v in s:
    d3[k].append(v)

print(d3.items())
# 결과 값
100
0
dict_items([('yellow', [1, 3]), ('blue', [2, 4]), ('red', [1])])

6.4 Counter

from collections import Counter

c = Counter('scientist')
print(c)

print(c['i'])
print(c['n'])
# 결과 값
Counter({'s': 2, 'i': 2, 't': 2, 'c': 1, 'e': 1, 'n': 1})
2
1
  • elements
    • 개수 만큼 풀어줌
from collections import Counter

c = Counter({'red': 4, 'blue': 2})
print(c)

print(list(c.elements()))
# 결과
Counter({'red': 4, 'blue': 2})
['red', 'red', 'red', 'red', 'blue', 'blue']
  • 키워드 방식
from collections import Counter


c = Counter(cats=4, dogs=6)
print(c)

print(list(c.elements()))
# 결과
Counter({'dogs': 6, 'cats': 4})
['cats', 'cats', 'cats', 'cats', 'dogs', 'dogs', 'dogs', 'dogs', 'dogs', 'dogs']
  • subtract
from collections import Counter


c = Counter(a=4, b=2, c=0, d=-2)
d = Counter(a=1, b=2, c=3, d=4)

print(c + d)  # 더하기
print(c & d)  # 적은 수
print(c | d)  # 큰 수

c.subtract(d)   # 빼기
print(c)
# 결과
Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2})
Counter({'b': 2, 'a': 1})
Counter({'a': 4, 'd': 4, 'c': 3, 'b': 2})
Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})

6.5 namedtuple

from collections import namedtuple

Point = namedtuple('my_point', ['x', 'y'])
p = Point(11, 22)
print(p)
print(p.x, p.y)
print(p[0], p[1])

연습

# 풀이 방법 1
from collections import defaultdict


text = """A press release is the quickest and easiest way to get free publicity. If
	 well written, a press release can result in multiple published articles about your
	firm and its products. And that can mean new prospects contacting you
    asking you to sell to them. ….""".lower().split()




text_set = set(text)
text_dict = defaultdict(int) # 디폴트 값이 0인 dictionary

print(text)
print(text_set)

for word_set in text_set:  ## [a, b, c, d]
    for word in text:      ## [a, a, a, b, b, c, c, d]
        if word == word_set:    ##
            text_dict[word_set] +=1

text_dict = sorted(text_dict.items(), key=lambda x:x[1], reverse=True)

print(text_dict)


# 풀이 방법 2
from collections import Counter


text = """A press release is the quickest and easiest way to get free publicity. If
	 well written, a press release can result in multiple published articles about your
	firm and its products. And that can mean new prospects contacting you
    asking you to sell to them. ….""".lower().split()

print(Counter(text))


# 풀이 방법 3
from collections import defaultdict


text = """A press release is the quickest and easiest way to get free publicity. If
	 well written, a press release can result in multiple published articles about your
	firm and its products. And that can mean new prospects contacting you
    asking you to sell to them. ….""".lower().split()

word_count = defaultdict(int)
for word in text:
    word_count[word] +=1


for k, v in sorted(word_count.items(), key=lambda x:x[1], reverse=True):
    print(k, v)

from collections import OrderedDict
for k, v in OrderedDict(sorted(word_count.items(), key=lambda x:x[1], reverse=True)).items():
    print(k, v)