mirror of
https://github.com/satwikkansal/wtfpython
synced 2024-11-11 14:08:53 +01:00
Merge 4be7c2a727
into 8135cbb99c
This commit is contained in:
commit
04f51b31c2
52
README.md
vendored
52
README.md
vendored
@ -1260,6 +1260,58 @@ False
|
|||||||
|
|
||||||
The built-in `ord()` function returns a character's Unicode [code point](https://en.wikipedia.org/wiki/Code_point), and different code positions of Cyrillic 'e' and Latin 'e' justify the behavior of the above example.
|
The built-in `ord()` function returns a character's Unicode [code point](https://en.wikipedia.org/wiki/Code_point), and different code positions of Cyrillic 'e' and Latin 'e' justify the behavior of the above example.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### ▶ Sets discriminating values? *
|
||||||
|
|
||||||
|
```py
|
||||||
|
>>> st = set()
|
||||||
|
>>> st.add(5)
|
||||||
|
>>> st.add(5)
|
||||||
|
>>> st.add(10)
|
||||||
|
>>> st.add(10)
|
||||||
|
>>> st.add(10)
|
||||||
|
>>> st.add(20)
|
||||||
|
>>> st.add(2)
|
||||||
|
>>> st.add(345678)
|
||||||
|
>>> st1 = set(sorted(st))
|
||||||
|
```
|
||||||
|
|
||||||
|
**Output:**
|
||||||
|
```py
|
||||||
|
>>> st
|
||||||
|
{2, 5, 10, 345678, 20}
|
||||||
|
>>> st1
|
||||||
|
{2, 5, 10, 345678, 20}
|
||||||
|
```
|
||||||
|
Everything looks pretty sorted ... just why are sets messing up with 345678?
|
||||||
|
|
||||||
|
|
||||||
|
#### 💡 Explanation:
|
||||||
|
|
||||||
|
* This is because a set object is "an unordered collection" of distinct objects.
|
||||||
|
* So values in a set object are not in a sorted way. Even the values 2, 5, 10 and 20 aren't inserted in sorted manner.
|
||||||
|
|
||||||
|
```py
|
||||||
|
>>> st = set()
|
||||||
|
>>> st.add(5)
|
||||||
|
>>> st.add(10)
|
||||||
|
>>> print(st)
|
||||||
|
>>> st.add(20)
|
||||||
|
>>> print(st)
|
||||||
|
>>> st.add(2)
|
||||||
|
>>> print(st)
|
||||||
|
>>> st.add(345678)
|
||||||
|
>>> print(st)
|
||||||
|
```
|
||||||
|
**Output:**
|
||||||
|
```py
|
||||||
|
{10, 5}
|
||||||
|
{10, 20, 5}
|
||||||
|
{10, 2, 20, 5}
|
||||||
|
{2, 5, 10, 345678, 20}
|
||||||
|
```
|
||||||
|
**Note:** This is why when we want to iterate on the distinct elements of a sequence in sorted way, we iterate on the list obtained from sorted(st), not on the set itself.
|
||||||
---
|
---
|
||||||
|
|
||||||
### ▶ Teleportation *
|
### ▶ Teleportation *
|
||||||
|
Loading…
Reference in New Issue
Block a user