Data Structure
Data Structure
์ ์ฉํ ์ฝ๋ ์ ๋ฆฌ
type
max, min
int, str,
list<append, pop, insert, del, index, reverse, sort>
sorted(some_dict.items(), lambda x: x[1], reverse=True)
reversed(some_list)
''.join(some_list)
len(some_list)
''.join(reversed(some_str))
some_str.startswith(prefix_str)
zip(items, items[1:])
enumerate(some_list)
range(0, len(some_list))
map(str, some_list)
map(''.join, permutations(items, 2))
map(''.join, combinations(items, 2))
์ฐพ๊ณ ์ ํ๋ ๋ฌธ์์ดA๊ฐ ๋ฌธ์์ดB์ ๋งจ ์์ ์๋์ง ์ฌ๋ถ
some_str = 'abcde'
print(some_str.startswith('ab'))
print(some_str.startswith('cde'))
True
False
์๋ค ๊ฐ ๋น๊ตํ๊ธฐ
items = [1,2,3]
for i in zip(items, items[1:]):
print(i)
(1, 2)
(2, 3)
๋ฌธ์์ด ๋ค์ง๊ธฐ
some_str = 'ABC'
print(''.join(reversed(some_str)))
CBA
๋ฆฌ์คํธ & ๋์ ๋๋ฆฌ ์ ๋ ฌ
some_list = {1, 2, 3}
print(sorted(some_list))
some_dict = {'a':1, 'b':2, 'c':3}
print(sorted(some_dict.items(), reverse=True))
tuple_dict = {'a':(1, 3), 'b':(2, 2), 'C':(3, 1)}
print(sorted(tuple_dict.items(), key=lambda x: x[1]))
[1, 2, 3]
[('c', 3), ('b', 2), ('a', 1)]
[('a', (1, 3)), ('b', (2, 2)), ('C', (3, 1))]
๊ฐ์ฅํฐ์ (๋ฌธ์์ด๋น๊ต๋ ์์คํค ๊ฐ์ผ๋ก ์นํ๋์ด ์ ๋ ฌ)
numbers = [12, 10, 22]
some_list = list(map(str, numbers))
print(some_list)
some_list.sort(key=lambda x: x*3, reverse=True)
print(some_list)
print(''.join(some_list))
['12', '10', '22']
['22', '12', '10']
221210
์์ด๊ณผ ์กฐํฉ
์์ด์ ์์๋๋ก ๋ฝ๋ ๊ฒ
from itertools import permutations
items = ['A', 'B', 'C']
print(list(permutations(items)))
print(list(map(''.join, permutations(items)))) # items์ ๋ชจ๋ ์์๋ฅผ ๊ฐ์ง๊ณ ์์ด์ ๋ง๋ ๋ค.
print(list(map(''.join, permutations(items, 2)))) # 2๊ฐ์ ์์๋ฅผ ๊ฐ์ง๊ณ ์์ด์ ๋ง๋ ๋ค
[('A', 'B', 'C'), ('A', 'C', 'B'), ('B', 'A', 'C'), ('B', 'C', 'A'), ('C', 'A', 'B'), ('C', 'B', 'A')]
['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']
['AB', 'AC', 'BA', 'BC', 'CA', 'CB']
์กฐํฉ์ ์์ด๊ณผ ๋ค๋ฅด๊ฒ ๋ฝ์์ ์์๋ฅผ ๊ณ ๋ คํ์ง ์๋ ๊ฒ ์๋ฅผ๋ค์ด, 1,2,3,4 ์ค 2๊ฐ์ ์ซ์๋ฅผ ๋ฝ์ ์์ฐ์๋ฅผ ๋ง๋๋ ๊ฒฝ์ฐ ์์ด์ 12,21๊ฐ ๋์ฌ ๋, ์กฐํฉ์ 12๋ง ๋์ด
from itertools import permutations, combinations
items = ['1', '2', '3']
print("์์ด:{}".format(list(map(''.join, permutations(items, 1)))))
print("์์ด:{}".format(list(permutations(items, 2))))
print("์์ด:{}".format(list(map(''.join, permutations(items, 2)))))
print("์กฐํฉ:{}".format(list(map(''.join, combinations(items, 1))))) # ์กฐํฉ์ ๋ง๋ค๋ ค๋ ์์ดํ
๊ณผ ์กฐํฉ์ ์๋ฅผ ๋ฐ๋์๋๊ฒจ์ค์ผํ๋ค.
print("์กฐํฉ:{}".format(list(combinations(items, 2))))
print("์กฐํฉ:{}".format(list(map(''.join, combinations(items, 2)))))
์์ด:['1', '2', '3']
์์ด:[('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), ('3', '1'), ('3', '2')]
์์ด:['12', '13', '21', '23', '31', '32']
์กฐํฉ:['1', '2', '3']
์กฐํฉ:[('1', '2'), ('1', '3'), ('2', '3')]
์กฐํฉ:['12', '13', '23']
์์์ฐพ๊ธฐ
def prime_check(n):
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
print(prime_check(1))
print(prime_check(2))
print(prime_check(3))
print(prime_check(9))
False
True
True
False
๋งค๋ฒ ์ ๋ ฌ์ด ํ์ํ ๊ฒฝ์ฐ ์ฌ์ฉ
import heapq
l = [3,2,1]
heapq.heapify(l)
print(l)
print(heapq.heappop(l))
print(l)
heapq.heappush(l, 4)
print(l)
[1,2,3]
1
[2,3]
[2,3,4]
Last updated
Was this helpful?