[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 |