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?