https://han-py.tistory.com/336
sorted(a) 변수가능
sorted(
data=['abc', 'abd', 'aaa', 'dac', 'dbb', 'dab']
data=sorted(data, key=lambda x: (x[1], x[2]))
print(data)
['aaa', 'dab', 'dac', 'dbb', 'abc', 'abd']
data=['abc', 'abd', 'aaa', 'dac', 'dbb', 'dab']
data=sorted(data, key=lambda x: (x[0], x[2]))
print(data)
['aaa', 'abc', 'abd', 'dbb', 'dab', 'dac']
data=['abc', 'abdba', 'aa', 'dbac', 'dbacccb', 'dab']
data=sorted(data, key=len)
print(data)
['aa', 'abc', 'dab', 'dbac', 'abdba', 'dbacccb']
array=[('가가가',50),('나나나',30),('다다다',74)]
print(sorted(array, key=lambda x: x[0]))
print(sorted(array, key=lambda x: x[1]))
[('가가가', 50), ('나나나', 30), ('다다다', 74)][('나나나', 30), ('가가가', 50), ('다다다', 74)]
array=[400,15,288,100]
array.sort(reverse=True)
array
[400, 288, 100, 15]
remove(값 하나만 삭제), 여러값 삭제( set 사용)
array=[1,2,3,4,5,6,7,8,9,10,24,35,65] remove=[i for i in range(20) if i%2==1] res=[i for i in array if i not in remove] res
[2, 4, 6, 8, 10, 24, 35, 65]
data=dict()
data['사과']='Apple'
data['바나나']='Banana'
data['코코넛']='Coconut'
if ('Apple' in data) or ('사과' in data):
print("apple 있다")
apple 있다
data=dict()
data['사과']='Apple'
data['바나나']='Banana'
data['코코넛']='Coconut'
if ('Apple' in data):
print("apple 있다")
출력값 없음
key_list=data.keys()
value_list=data.values()
for key in key_list:
print(data[key]) # 값을 출력
print(key)
print("-----------------------")
for values in value_list:
print(values)
Apple
사과
Banana
바나나
Coconut
코코넛
Apple
Banana
Coconut
data=set([1,1,1,1,3,4])
data={1,1,1,1,3,4}
print(data)
{1,3,4}
: .add(), .update(), .remove()
print(5,6,7, end=" ")
print(8)
print(9)
print(10)
5 6 7 8
9
10
score=[90,85,77,65,97]
cheating_student_list={2,4}
for i in range(5):
if i+1 in cheating_student_list:
print(i+1, "불합격")
continue
if score[i]>=80:
print(i+1, "번 합격")
1 번 합격
2 불합격
4 불합격
5 번 합격
score=[90,85,77,65,97]
cheating_student_list={2,4}
for i in range(5):
if i+1 in cheating_student_list:
break
if score[i]>=80:
print(i+1, "번 합격")
1 번 합격
: 순열,조합
from itertools import permutations
result=list(permutations(data,2))
from itertools import product
result=list(product(data, repeat=2))
from itertools import combinations_with_replacement
list(combinations_with_replacement(data,2))
: deque, counter
from collections import Counter
count=Counter(['red','blue','blue','red','green'])
print(count['blue'])
: 우선순위 큐
: 이진탐색
import math
print(math.gcd(12,16)) #gcd는 math에 있음
# lcm은 직접 만들어 써야함
# 최소공배수
def lcm(a,b):
return a*b//math.gcd(a,b)
n,k=map(int,input().split())
cnt=0
while 1:
# 뺄셈
t=(n//k)*k #t는 k의 배수
cnt+=(n-t) # 배수가 되기 전까진 1씩 빼줘야 함, 차이만큼 더함
n=t # 배수를 만들어버렸으므로 n=t
# N이 k보다 작아지면 탈출
if n<k:
break
# 나눗셈
cnt+=1 #k를 나누는 연산을 추가
n//=k
# 연산이 추가 되었으므로 cnt에서 1을 뺄 것
cnt+=(n-1) # 아직 n!=1 이므로 빼준다
print(cnt)
s=input()
res=int(s[0])
for i in range(1,len(s)):
n=int(s[i])
print(n)
if n<=1 or res<=1:
res+=n
else:
res*=n
print(res)
n=int(input())
data=list(map(int, input().split()))
data.sort()
res=0 # 총 그룹의 수
cnt=0 # 현재 그룹에 포함된 모험가의 수
for i in data:
cnt+=1
if cnt>=i:
res+=1
cnt=0
print(res)
5
(1 / 2 2 )/ 2 3
6
(1/ 1)/ 2 3 4
def solution(numbers):
ans=0
for i in range(10):
if i not in numbers:
ans+=i
return ans
def solution(n, lost, reserve):
reserve_set = set(reserve)-set(lost)
lost_set = set(lost)-set(reserve)
for reserve_person in reserve_set:
if reserve_person-1 in lost_set:
lost_set.remove(reserve_person-1)
elif reserve_person+1 in lost_set:
lost_set.remove(reserve_person+1)
return n-len(lost_set)