*Memo for a string, bytes and bytearray:
ord() can get a Unicode code point from a character or byte:
*Memo:
- The 1st argument is
c
(Required-Type:str
or Bytes-like object except memoryview):- Don't use
c=
.
- Don't use
- In addition, chr() can get a character from a Unicode code point:
- The 1st argument is
i
(Required-Type:int
):- Don't use
i=
.
- Don't use
- The 1st argument is
<String & Bytes & Bytearray>:
print(ord('A')) print(ord(b'A')) print(ord(bytearray(b'A'))) # 65
print(chr(65)) # A
sorted() can convert a string, bytes and bytearray to a list, then sort the list, then the sorted list is converted to a string, bytes and bytearray with join(), bytes() and bytearray() respectively as shown below:
*Memo:
- The 1st argument is
iterable
(Required-Type:Iterable):- Don't use
iterable=
.
- Don't use
- The 2nd argument is
key
(Optional-Default:None
-Type:Callable or NoneType). - The 3rd argument is
reverse
(Optional-Default:False
-Type:bool
) to reverse the list. -
sorted()
creates a copy:- Be careful,
sorted()
does shallow copy instead of deep copy as my issue.
- Be careful,
<String>:
v = 'DAECB' print(sorted(v)) # ['A', 'B', 'C', 'D', 'E'] print(''.join(sorted(v))) # ABCDE print(''.join(sorted(v, reverse=True))) # EDCBA
v = 'DaEcB' """ Case sensitive sort """ print(''.join(sorted(v))) # BDEac """ Case insensitive sort """ print(''.join(sorted(v, key=str.upper))) print(''.join(sorted(v, key=str.lower))) # aBcDE
<Bytes & Bytearray>:
bytes:
v = b'DAECB' print(sorted(v)) # [65, 66, 67, 68, 69] print(bytes(sorted(v))) # b'ABCDE' print(bytes(sorted(v, reverse=True))) # b'EDCBA'
v = b'DaEcB' def upper(b): return chr(b).upper() def lower(b): return chr(b).upper() """ Case sensitive sort """ print(bytes(sorted(v))) # b'BDEac' """ Case insensitive sort """ print(bytes(sorted(v, key=upper))) print(bytes(sorted(v, key=lower))) # b'ABCDE'
bytearray:
v = bytearray(b'DAECB') print(sorted(v)) # [65, 66, 67, 68, 69] print(bytearray(sorted(v))) # bytearray(b'ABCDE') print(bytearray(sorted(v, reverse=True))) # bytearray(b'EDCBA')
v = bytearray(b'DaEcB') def upper(b): return chr(b).upper() def lower(b): return chr(b).upper() """ Case sensitive sort """ print(bytearray(sorted(v))) # bytearray(b'BDEac') """ Case insensitive sort """ print(bytearray(sorted(v, key=upper))) print(bytearray(sorted(v, key=lower))) # bytearray(b'aBcDE')
reversed() can return the iterator which has the reversed characters of a string and the reversed bytes of a bytes and bytearray, then the iterator is converted to a string, bytes and bytearray with join()
, bytes()
and bytearray()
respectively as shown below:
*Memo:
- The 1st argument is
seq
(Required-Type:Sequence):- Don't use
seq=
.
- Don't use
<String>:
v = 'DAECB' print(reversed(v)) # <reversed object at 0x000001F3B9D83C10> print(''.join(reversed(v))) # BCEAD
<Bytes & Bytearray>:
bytes:
v = b'DAECB' print(bytes(reversed(v))) # b'BCEAD'
bytearray:
v = bytearray(b'DAECB') print(bytearray(reversed(v))) # bytearray(b'BCEAD')
Top comments (0)