๋ค์ด๋ฒ ๋ถ์คํธ์ฝ์ค [์ธ๊ณต์ง๋ฅ ๊ธฐ์ด ๋ค์ง๊ธฐ] ๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ์์ฑํ์์ต๋๋ค.
#Last In First Out (LIFO ๋ฆฌํฌ ๊ตฌ์กฐ)
#data push(input), data pop(output)
a = [1,2,3,4,5]
a.append(10)
#[1, 2, 3, 4, 5, 10]
a.append(20)
#[1, 2, 3, 4, 5, 10, 20]
a.pop()
a = [1,2,3,4,5]
a.append(10)
a.append(20)
a.pop(0)
๋์ ๋๋ฆฌ ํ์ ์ ํ์ฉํด์ ๋ฐ์ดํฐ ๋ถ์ ์ค์ต์ ํด๋ณด์
๊ฐ์์๋ฃ์์ ์ ๊ณตํ csv ํ์ผ์ ๋ค์ด ๋ฐ์ ํ ์งํํ๋ค.
์ฌ์ฉํ ํด์ ๋น์ฃผ์ผ ์ฝ๋!!
์ฃผํผํฐ ๋
ธํธ๋ถ์ผ๋ก ํ๋ค๊ฐ ์๋ผ์ ๋น์ฃผ์ผ ์ฝ๋๋ก ๋ฐ๊ฟจ๋ค.
command_data = []
with open('command_data.csv','r', encoding="utf8") as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar = '"')
for row in spamreader:
command_data.append(row)
command_counter = {} #dict ์์ฑ
for data in command_data: #list ๋ฐ์ดํฐ๋ฅผ dict๋ก ๋ณ๊ฒฝ
if data[1] in command_counter.keys():
command_counter[data[1]] += 1
else:
command_counter[data[1]] = 1
์ฌ๊ธฐ๊น์ง๋ data ๊ฐ์ ์ธ๋ ๊ฑฐ์ฌ์, ํฐ๋ฏธ๋์ ๊ฒฐ๊ณผ๊ฐ์ ํ์ธํด๋ณด๋ฉด ๊ฐ key๊ฐ๋ค์ด ๋ช ๋ฒ ๋ฑ์ฅํ๋์ง ์ถ๋ ฅ๋์ด์์์ ์ ์ ์๋ค.
dictlist = [] #dict๋ฅผ list๋ก ๋ณ๊ฒฝ
for key, value in command_counter.items():
temp = [key, value]
dictlist.append(temp)
sorted_dict = sorted(dictlist, key=getKey, reverse = True)
print(sorted_dict[:100])
์ด ๋ถ๋ถ์ dictlist์ key์ command_counter(๊ฐ์)๋ฅผ ์ ๋ ฌํด๋์ ์ฝ๋์ด๋ค. ๊ฐ์๊ฐ ๋ง์ ์์ผ๋ฅด๋ก ์์ 100๊ฐ ๋ด๋ฆผ์ฐจ์(reverse = True) ์ ๋ ฌํด๋์๋ค.
deque_list = deque()
for i in range(5):
deque_list.append(i)
print(deque_list)
//deque([0, 1, 2, 3, 4])
deque_list.appendleft(10)
print(deque_list)
//deque([10, 0, 1, 2, 3, 4])
deque_list.rotate(2)
print(deque_list)
//deque([3, 4, 10, 0, 1, 2])
deque_list.rotate(2)
print(deque_list)
//deque([1, 2, 3, 4, 10, 0])
print(deque(reversed(deque_list)))
//deque([0, 10, 4, 3, 2, 1])
deque_list.extend([5,6,7])
print(deque_list)
deque_list.extendleft([5,6,7])
print(deque_list)
//deque([1, 2, 3, 4, 10, 0, 5, 6, 7])
//deque([7, 6, 5, 1, 2, 3, 4, 10, 0, 5, 6, 7])
//
Stack 0.1875 seconds
General List 0.546875 seconds
//
defaultdict
- Dict type์ ๊ฐ์ ๊ธฐ๋ณธ ๊ฐ์ ์ง์
- ์ ๊ท๊ฐ ์์ฑ ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
d = dict()
print(d["first"])
# -> Key Error
from collections import defaultdict
d = defaultdict(object) #Default dictionary ์์ฑ
d = defaultdict(lambda: 0) #Default = 0
print(d["first"])
# > 0
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.""".lower().split()
print(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.']
์ค์ต~
from collections import Counter
ball_or_strike_list = ["B","S","S","B","S","B","B"]
c = Counter(ball_or_strike_list)
print(c)
c = Counter({'red':4, 'blue':2})
print(list(c.elements()))
//Counter({'B': 4, 'S': 3})
//['red', 'red', 'red', 'red', 'blue', 'blue']
from collections import namedtuple
Point = namedtuple('Point', ['x','y'])
p = Point(11, y = 22)
print(p[0] + p[1])
# > 33
x,y = p
print(x,y)
# > 11 22
print(p.x + p.y)
# > 33
print(Point(x=11, y=22))
# > Point(x=11, y=22)