파이썬 리스트 끼리의 대소비교

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