반응형
상황
스트링으로 되어있는 비교 연산자를 사용해서 비교를 할 수 있을까?
해결
import operator
def check(left, string_operator, right):
operators = {'>': operator.gt,
'<': operator.lt,
'>=': operator.ge,
'<=': operator.le,
'=': operator.eq}
return operators[string_operator](left, right)
print(check(3, '>', 2)) # True
print(check(3, '<', 2)) # False
print(check(3, '>=', 2)) # True
print(check(3, '<=', 2)) # False
print(check(3, '=', 2)) # False
반응형
'python' 카테고리의 다른 글
[python] string format (0) | 2020.03.19 |
---|---|
[Python] python3으로 업그레이드 (mac) (0) | 2020.02.28 |
[python][pyjwt] JWT example (0) | 2020.02.03 |
[python] AttributeError: module 'yaml' has no attribute 'FullLoader' (0) | 2020.01.30 |
[Python] python3으로 업그레이드 (centos7) (0) | 2020.01.14 |