이 게시글은 이 레포지토리를 공부하며 작성했습니다.
a, b = map(int, input().split())
first, *rest, last = list
print(*list)
list = [i for i in range(10)]
print(*[1 if dt in _set else 0 for dt in _list], sep='\n')
print([[x ** 2 for x in range(3)] for _ in range(3)])
수의 반복이 의미 없을 때 > set()
사용
data_set = set(data)
*dict.keys()
*dict.values()
*dict.items()
java.stream()
와 비슷
test_list = ['Test', 'test', 'TEST', 'tteesstt']
converted_list = list(set(map(lambda string: string.lower(), test_list))) # test, tteesstt
_dict = dict(zip(fruit, price))
_dict.setdefault('strawberry', 0))
setdefault()
- 딕셔너리에 값에 있을 땐 해당 값을 리턴. 값이 없을 땐 두번째 인자로 넘겨준 값을 추가하고 추가한 값을 리턴setdefault
를 암묵적으로 실행from collections import defaultdict
movie_review = [('Train to Busan', 4), ('Clementine', 5), ('Parasite', 4.5), ('Train to Busan', 4.2), ('Train to Busan', 4.5), ('Clementine', 5)]
index = defaultdict(list)
defaultdict
의 인자값이 나옴print(index['nonexistent key'])
>> []
sort()
- 리스트 내부 정렬
_list.sort()
set에는 sort()
말고 sorted()
을 사용하자
sorted()
- 컨테이너형 데이터를 받아 정렬하여 돌려줌
sorted(_list)
sorted_list = sorted(_list, reveresd=True)
리스트의 각 원소가 튜플일 때, 튜플의 두번째 값 기준으로 오름차순 정렬
sorted_list = sorted(_list, key = lambda dt: dt[1]
리스트의 각 원소가 튜플일 때, 튜플의 첫번째 값 기준으로 오름차순 정렬하고, 값이 같으면 두번째 값으로 내림차순 정렬
sorted_list = sorted(_list, key = lambda dt: (dt[1], -dt[0])) # (8, 2), (1, 3), (2, 5), (4, 7)
strip()
- 문자열 제거
stringVar.strip()
슬라이싱: string[시작:종료(포함 안함):간격]
print(string[::-1])
print("".join(reversed(string)))
for i in ~
구조 또는 join
을 함께 사용해야 한다Combination: 중복 없음. 순서 구분 X.
Permutation: 중복 없음. 순서 구분.
import itertools
iter = itertools.combinations(_list, 2)
iter = itertools.permutations(_list, 2)
iter = itertools.combinations_with_replacement(_list, 2)
iter = itertools.product(_list, repeat=2)
combinations_with_replacement
- 중복 가능 조합
product
- 모든 가능한 경우의 수 (Cartesian Product)
sum()
iterable의 문자열들을 이어붙인 문자열을 리턴한다
print(', '.join(_list))
a, b = b, a
for idx, val in enumerate(_list):
print(idx, val)
enumerate(_list)
- (인덱스, 값)의 형태의 튜플 리턴from collections import Counter
Counter('hello world') # Counter({'l':3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
.most_common(n)
- 가장 빈도수가 높은 n개의 문자열의 튜플 리턴
from collections import Counter
Counter('hello world').most_common() #[('l', 3), ('o', 2), ('h', 1), ('e', 1), (' ', 1), ('w', 1), ('r', 1), ('d', 1)]
Counter('hello world').most_common(2) #[('l', 3), ('o', 2)]