파이썬 리스트 끼리의 대소비교
2022. 8. 2. 01:34ㆍ파이썬/파이썬 일반 꿀팁
num_1=[2,8,3,4,5]
num_2=[4,6,3,1,3]
class comparable_list(list):
def __lt__(self, other):
return all(x < y for x, y in zip(self, other))
def __gt__(self, other):
return all(x > y for x, y in zip(self, other))
n1 = comparable_list(num_1);
n2 = comparable_list(num_2);
print(n1 < n2) # False
n3 = comparable_list([9,9,9,9,9]);
print(n1 < n3) # True
n4 = comparable_list([0,0,0,0,0]);
print(n1 > n4) # True
'파이썬 > 파이썬 일반 꿀팁' 카테고리의 다른 글
더미변수 다시 원래대로 돌리기 (0) | 2022.07.11 |
---|---|
리스트 순서 유지하면서 중복 제거 방법 (0) | 2022.05.12 |
딕셔너리를 활용해서 변수 생성 (0) | 2022.04.05 |
itertools (순열, 조합) (0) | 2021.06.30 |
반복문에서 zip 활용 (0) | 2021.06.01 |