알고리즘/basic
-
[Python] 리스트 내 원소 대소 비교, 조합 2023.02.05
-
[Python] 특정 값에 해당하는 모든 인덱스 찾기 2023.02.04
-
[Python] 특정 알파벳에서 n번째 알파벳 구하기 (아스키코드) 2023.02.03
-
[Python] input, sys.stdin.readline 차이점 2023.02.02
[Python] 리스트 내 원소 대소 비교, 조합
2023. 2. 5. 14:58
li=[1,3,2,5,4]
for now, next in zip(li,li[1:]):
print(now, next)
📌각 인덱스마다 모든 원소 대소 비교
1. 이중 for문
li = [-1, 6, 3, 4, 7, 4]
for i in range(len(li)-1):
now = li[i]
next = li[i+1:]
for n in range(len(next)):
if now > next[n]:
print(now)
2. permutations ( 모든 조합, 중복 조합 포함 O )
from itertools import permutations
nums = [1, 2, 3, 4]
print(list(permutations(nums, 2)))
3. combinations ( 중복 조합 포함 X )
from itertools import combinations
nums = [1, 2, 3, 4]
print(list(combinations(nums, 2)))
4. product : 두 개 이상의 리스트 ( 중복 조합 포함 X)
from itertools import product
nums = [[1, 2, 3], ['a', 'b'], ['ㄱ','ㄴ', 'ㄷ']]
print(list(product(*nums)))
'알고리즘 > basic' 카테고리의 다른 글
[Python] 특정 값에 해당하는 모든 인덱스 찾기 (0) | 2023.02.04 |
---|---|
[Python] 특정 알파벳에서 n번째 알파벳 구하기 (아스키코드) (0) | 2023.02.03 |
[Python] input, sys.stdin.readline 차이점 (0) | 2023.02.02 |
[Python] 특정 값에 해당하는 모든 인덱스 찾기
2023. 2. 4. 15:57
li=[1,0,1]
#filter 사용
list(filter(lambda e:li[e] == 1, range(len(li))))
#enumerate 사용
[i for i, ele in enumerate(li) if ele == 1]
'알고리즘 > basic' 카테고리의 다른 글
[Python] 리스트 내 원소 대소 비교, 조합 (0) | 2023.02.05 |
---|---|
[Python] 특정 알파벳에서 n번째 알파벳 구하기 (아스키코드) (0) | 2023.02.03 |
[Python] input, sys.stdin.readline 차이점 (0) | 2023.02.02 |
[Python] 특정 알파벳에서 n번째 알파벳 구하기 (아스키코드)
2023. 2. 3. 15:43
아스키코드 활용
print(chr(ord('a') + (ord('z') + 5 - ord('a')) % 26))
z에서 5번째 후 알파벳 구하기
'알고리즘 > basic' 카테고리의 다른 글
[Python] 리스트 내 원소 대소 비교, 조합 (0) | 2023.02.05 |
---|---|
[Python] 특정 값에 해당하는 모든 인덱스 찾기 (0) | 2023.02.04 |
[Python] input, sys.stdin.readline 차이점 (0) | 2023.02.02 |
[Python] input, sys.stdin.readline 차이점
2023. 2. 2. 10:38
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
graph = []
for i in range(n):
graph.append(list(map(int, input().rstrip())))
빠르게 입력 받기 위해 input보다는 sys.stdin.readline을 사용한다.
graph 입력을 아래와 같은 코드로 입력 받았다.
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
graph = []
for i in range(n):
graph.append(list(map(int, input())))
코드를 실행한 후
3 3
010 을 입력하자
ValueError: invalid literal for int() with base 10: '\n'오류가 발생했다.
📌input 과 sys.stdin.readline
input : 입력에 rstrip() 후 리턴
sys.stdin.readline : 개행문자(\n)를 포함한 값 리턴
따라서 아래와 같은 코드로 바꿔줘야 한다.
import sys
input = sys.stdin.readline
n,m = map(int, input().split())
graph = []
for i in range(n):
graph.append(list(map(int, input().rstrip())))
'알고리즘 > basic' 카테고리의 다른 글
[Python] 리스트 내 원소 대소 비교, 조합 (0) | 2023.02.05 |
---|---|
[Python] 특정 값에 해당하는 모든 인덱스 찾기 (0) | 2023.02.04 |
[Python] 특정 알파벳에서 n번째 알파벳 구하기 (아스키코드) (0) | 2023.02.03 |