Comments (3)
As shiracamus and lezcano have already mentioned, pretty much every other language uses the same implementation as it is consistent with the general idea of lexicographical ordering. If you really want you could create a custom class and maybe override how the comparison operators work for specific use case.
class CustomList(list):
def __le__(self, other):
return all(x <= y for x, y in zip(self, other))
def __ge__(self, other):
return all(x >= y for x, y in zip(self, other))
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))
def test_custom_list():
assert CustomList([0, 0, 0]) <= CustomList([255, 123, 5]) <= CustomList([255, 255, 255])
assert not CustomList([0, 0, 0]) <= CustomList([256, 123, 5]) <= CustomList([255, 255, 255])
assert CustomList([0, 0, 0]) <= CustomList([255, 123, -5]) <= CustomList([255, 255, 255])
assert not CustomList([0]) <= CustomList([-5]) <= CustomList([255])
assert not CustomList([0, 0, 0]) <= CustomList([-5, 123, 2555]) <= CustomList([255, 255, 255])
# You can run these test cases
test_custom_list()
In this example, I created a Custom class that inherits from the built-in list class and overrides the less than or equal (le), greater than or equal (ge), less than (lt), and greater than (gt) comparison methods.
from wtfpython.
The same result is obtained in JavaScript.
> [0, 0, 0] <= [255, 123, 5] && [255, 123, 5] <= [255, 255, 255]
true
> [0, 0, 0] <= [256, 123, 5] && [256, 123, 5] <= [255, 255, 255]
false
> [0, 0, 0] <= [255, 123, -5] && [255, 123, -5] <= [255, 255, 255]
true
> [0] <= [-5] && [-5] <= [255]
false
> ([0, 0, 0] <= [255, 123, -5]) && ([255, 123, -5] <= [255, 255, 255])
true
> [0, 0, 0] <= [-5, 123, 2555] && [-5, 123, 2555] <= [255, 255, 255]
false
from wtfpython.
Python uses the lexicographic order, same as C++ and most languages really.
from wtfpython.
Related Issues (20)
- floating point operations and test HOT 1
- Add translation for German HOT 8
- Wt
- Python Interning operation result
- Typo in [Strings and the backslashes], missing right parenthesis HOT 3
- Someone to collaborate on the next major release
- Image for tricky strings is not visible in dark mode
- Weird arithmetic with multiple `+` and `-` HOT 3
- StopIteration sometimes gets transformed to RuntimeError (PEP 0479)
- Add translation for persian
- A f**king problem with sys.path
- Persian
- N
- Create application
- Python Inner class raises recursion error HOT 3
- G HOT 1
- Autoricambi
- Ggg
- Feature: New Documentation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wtfpython.