ŠŠ¾Š½ŃŠµŠ¹Š½ŠµŃ ā Š¾Š±ŃŠµŠŗŃ, ŠŗŠ¾ŃŠ¾ŃŃŠ¹ ŃŠ¾Š“ŠµŃŠ¶ŠøŃ Š²Š½ŃŃŃŠø ŃŠµŠ±Ń Š“ŃŃŠ³ŠøŠµ Š¾Š±ŃŠµŠŗŃŃ. Š¢ŠµŃ
Š½ŠøŃŠµŃŠŗŠø Š²ŃŠµ ŠŗŠ¾Š½ŃŠµŠ¹Š½ŠµŃŃ Š½Š°ŃŠ»ŠµŠ“ŃŃŃ Š¾Ń Collections.abc.Container
Š¼ŠµŃŠ¾Š“ __contains__
. Š” ŃŃŠøŠ¼ Š¼ŠµŃŠ¾Š“Š¾Š¼ ŃŠ°Š±Š¾ŃŠ°ŠµŃ Š¾ŠæŠµŃŠ°ŃŠ¾Ń in
.
ŠŠ¾Š½ŃŠµŠ¹Š½ŠµŃŃ ŠæŃŠµŠ“ŃŃŠ°Š²Š»ŠµŠ½Ń ŃŠ°Š·Š½ŃŠ¼Šø ŃŃŃŃŠŗŃŃŃŠ°Š¼Šø Š“Š°Š½Š½ŃŃ . ŠŠ° ŃŃŠøŃ ŃŃŃŃŠŗŃŃŃŠ°Ń Š¼Š¾Š¶Š½Š¾ ŠæŠ¾ŃŃŃŠ¾ŠøŃŃ Š²ŃŠµŠ²Š¾Š·Š¼Š¾Š¶Š½ŃŠµ Š°Š±ŃŃŃŠ°ŠŗŃŃŠµ ŃŠøŠæŃ Š“Š°Š½Š½ŃŃ .
NB! ŠŠµ Š½Š°Š“Š¾ ŠæŃŃŠ°ŃŃ ŃŠøŠæŃ Š“Š°Š½Š½ŃŃ ŃŠ¾ ŃŃŃŃŠŗŃŃŃŠ°Š¼Šø Š“Š°Š½Š½ŃŃ Šø Š°Š±ŃŃŃŠ°ŠŗŃŠ½ŃŠ¼Šø ŃŠøŠæŠ°Š¼Šø Š“Š°Š½Š½ŃŃ .
Š¢ŠøŠæ Š“Š°Š½Š½ŃŃ
āĀ Ń
Š°ŃŠ°ŠŗŃŠµŃŠøŃŃŠøŠŗŠ° ŠŗŃŃŠ¾ŃŠŗŠ° Š“Š°Š½Š½ŃŃ
, ŠŗŠ¾ŃŠ¾ŃŠ°Ń Š¾ŠæŃŠµŠ“ŠµŠ»ŃŠµŃ ŃŠæŠ¾ŃŠ¾Š± ŠµŠ³Š¾ ŠøŠ½ŃŠµŃŠæŃŠµŃŠ°ŃŠøŠø.
ŠŠ°ŠæŃŠøŠ¼ŠµŃ, b1100001
Š¼Š¾Š¶ŠµŃ Š±ŃŃŃ ŠŗŠ°Šŗ ŃŠøŃŠ»Š¾ 97 (ŃŠµŠ»ŃŠ¹ ŃŠøŠæ), Š° Š¼Š¾Š¶ŠµŃ Š±ŃŃŃ ŠŗŠ°Šŗ ŃŠøŠ¼Š²Š¾Š» a
ŠøŠ· ASCII (ŃŠøŠ¼Š²Š¾Š»).
Š”ŃŃŃŠŗŃŃŃŠ° Š“Š°Š½Š½ŃŃ āĀ Š½Š°Š±Š¾Ń ŠøŠ· ŠæŃŠøŠ¼ŠøŃŠøŠ²Š¾Š² Š“Š°Š½Š½ŃŃ Šø Š¾ŠæŠµŃŠ°ŃŠøŠ¹ Š½Š°Š“ Š½ŠøŠ¼Šø, Š¾Š³ŃŠ°Š½ŠøŠ·Š¾Š²Š°Š½Š½ŃŠµ Š“Š»Ń ŃŃŃŠµŠŗŃŠøŠ²Š½Š¾Š³Š¾ ŃŠµŃŠµŠ½ŠøŃ Š·Š°Š“Š°ŃŠø. ŠŠ°ŠæŃŠøŠ¼ŠµŃ, Š¼Š°ŃŃŠøŠ²) ŠøŠ»Šø ŃŠ²ŃŠ·Š½ŃŠ¹ ŃŠæŠøŃŠ¾Šŗ.
ŠŠ±ŃŃŃŠ°ŠŗŃŠ½ŃŠ¹ ŃŠøŠæ Š“Š°Š½Š½ŃŃ (ŠŠ¢Š) āĀ Š¼Š°ŃŠµŠ¼Š°ŃŠøŃŠµŃŠŗŠ°Ń Š¼Š¾Š“ŠµŠ»Ń ŃŃŃŃŠŗŃŃŃŃ Š“Š°Š½Š½ŃŃ , ŠµŠµ ŠøŠ½ŃŠµŃŃŠµŠ¹Ń. ŠŠ°ŠæŃŠøŠ¼ŠµŃ, Š¾ŃŠµŃŠµŠ“Ń ā Š°Š±ŃŃŃŠ°ŠŗŃŠ½ŃŠ¹ ŃŠøŠæ Š“Š°Š½Š½ŃŃ , ŠŗŠ¾ŃŠ¾ŃŃŠ¹ Š¼Š¾Š¶Š½Š¾ ŃŠµŠ°Š»ŠøŠ·Š¾Š²Š°ŃŃ Š½Š° ŃŠ°Š·Š½ŃŃ ŃŃŃŃŠŗŃŃŃŠ°Ń Š“Š°Š½Š½ŃŃ .
Š Python ŠæŃŠøŃŠ²Š°ŠøŠ²Š°Š½ŠøŠµ =
ŠŗŠ¾ŠæŠøŃŃŠµŃ ŃŠ¾Š»ŃŠŗŠ¾ ŃŃŃŠ»ŠŗŠø Š¾Š±ŃŠµŠŗŃŠ¾Š². Š Š¾ŠæŃŠµŠ°ŃŠøŃ Š²ŠøŠ“Š° name = obj
Š¼Š¾Š¶Š½Š¾ ŠøŠ½ŃŠµŃŠæŃŠµŃŠøŃŠ¾Š²Š°ŃŃ ŠŗŠ°Šŗ ŠæŃŠøŃŠ²Š°ŠøŠ²Š°Š½ŠøŠµ Š¾Š±ŃŠµŠŗŃŃ obj
ŠøŠ¼ŠµŠ½Šø name
. ŠŠ°Šŗ Š² ŃŠ°ŠŗŠ¾Š¼ ŃŠ»ŃŃŠ°Šµ ŃŠŗŠ¾ŠæŠøŃŠ¾Š²Š°ŃŃ Š¾Š±ŃŠµŠŗŃ?
ŠŠ»Ń ŠæŃŠøŠ¼ŠµŃŠ°, Š“Š°Š²Š°Š¹ŃŠµ ŠæŠ¾ŠæŃŠ¾Š±ŃŠµŠ¼ ŃŠŗŠ¾ŠæŠøŃŠ¾Š²Š°ŃŃ ŃŠæŠøŃŠ¾Šŗ ŃŠæŠøŃŠŗŠ¾Š².
# ŠŃŠøŠ¼ŠµŃ ŃŠæŠøŃŠŗŠ° ŃŠæŠøŃŠŗŠ¾Š²
a = [1,2,3]
b = [4,5,6]
c = [a,b] # ŃŠ¾Š“ŠµŃŠ¶ŠøŃ ŃŃŃŠ»ŠŗŠø Š½Š° a Šø b
ŠŃŠ»Šø Š¼Ń ŃŠæŠøŃŠ¾Šŗ c
ŠæŃŠøŃŠ²Š¾ŠøŠ¼ ŃŠæŠøŃŠŗŃ d
, ŃŠ¾ ŠæŠ¾ ŃŠ°ŠŗŃŃ Ń ŃŠæŠøŃŠŗŠ° [a,b]
ŠæŃŠ¾ŃŃŠ¾ Š±ŃŠ“ŠµŃ Š“Š²Š° ŠøŠ¼ŠµŠ½ŠøŠµ c
Šø d
.
# ŠŠ°Š²ŠµŃŠøŠ²Š°ŠµŠ¼ Š½Š¾Š²Š¾Šµ ŠøŠ¼Ń ŃŠæŠøŃŠŗŃ Ń (Ń Š¾Š±ŠµŃŠµŠŗŃŠ¾Š² Š“Š°Š¾Š»Š¶Š½Ń Š±ŃŃŃ Š¾Š“ŠøŠ½Š°ŠŗŠ¾Š²ŃŠµ id)
d = c
id(c), id(d)
ŠŠ¾Š¶Š½Š¾ ŃŠŗŠ¾ŠæŠøŃŠ¾Š²Š°ŃŃ ŃŠæŠøŃŠ¾Šŗ, ŃŠ¾Š·Š“Š°Š² ŃŠ²Š½Š¾ Š½Š¾Š²ŃŠ¹ Ń ŃŠ¾Š“ŠµŃŠ¶ŠøŠ¼ŃŠ¼ ŃŃŠ°ŃŠ¾Š³Š¾. ŠŃŠø ŃŃŠ¾Š¼ ŠæŃŠ¾ŠøŃŃ Š¾Š“ŠøŃ ŠæŠ¾Š²ŠµŃŃ Š½Š¾ŃŃŠ½Š¾Šµ ŠŗŠ¾ŠæŠøŃŠ¾Š²Š°Š½ŠøŠµ ŃŠæŠøŃŠŗŠ°. ŠŠ»ŠµŠ¼ŠµŠ½ŃŃ ŃŠŗŠ°Š·ŃŠ²Š°ŃŃ Š½Š° ŃŠ»ŠµŠ¼ŠµŠ½ŃŃ ŃŃŠ°ŃŠ¾Š³Š¾ ŃŠæŠøŃŠŗŠ°.
# ŠŠ¾Š²ŠµŃŃ
Š½Š¾ŃŃŠ½Š¾Šµ ŠŗŠ¾ŠæŠøŃŠ¾Š²Š°Š½ŠøŠµ ŃŠæŠøŃŠŗŠ° (ŃŠæŠøŃŠŗŠø ŃŠ°Š·Š½ŃŠµ, Š½Š¾ Š²Š½ŃŃŃŠµŠ½Š½ŠøŠµ Š¾Š±ŃŠµŠŗŃŃ Š½ŠµŃ)
d = list(c)
id(c), id(d), id(c[0]), id(d[0])
Š Š°Š·Š»ŠøŃŠ°ŃŃ Š“Š²Š° ŃŠøŠæŠ° ŠŗŠ¾ŠæŠøŃŠ¾Š²Š°Š½ŠøŃ:
Š Š¾ŃŠ»ŠøŃŠøŠø Š¾Ń ŠæŠ¾Š²ŠµŃŃ Š½Š¾ŃŃŠ½Š¾Š³Š¾ ŠŗŠ¾ŠæŠøŃŠ¾Š²Š°Š½ŠøŃ, Š³Š»ŃŠ±Š¾ŠŗŠ¾Šµ Š¾Š±Ń Š¾Š“ŠøŃ Š¾Š±ŃŠµŠŗŃ ŃŠµŠŗŃŃŃŠøŠ²Š½Š¾ Šø ŠŗŠ¾ŠæŠøŃŃŠµŃ Š²ŃŠµ ŠæŠ¾Š“Š¾Š±ŃŠµŠŗŃŃ.
# ŠŠ»ŃŠ±Š¾ŠŗŠ¾Šµ ŠŗŠ¾ŠæŠøŃŠ¾Š²Š°Š½ŠøŠµ ŃŠæŠøŃŠŗŠ°
from copy import deepcopy
d = deepcopy(c)
id(c), id(d), id(c[0]), id(d[0])
ŠŃŃŃŠ¾ŠµŠ½Š½ŃŠµ Š² Python-ŠŗŠ¾Š½ŃŠµŠ¹Š½ŠµŃŃ Š¼Š¾Š¶Š½Š¾ ŃŠ°Š·Š“ŠµŠ»ŠøŃŃ Š½Š° Š“Š²Šµ ŠŗŠ°ŃŠµŠ³Š¾ŃŠøŠø:
string
, tuple
, range
, frozenset
, bytes
list
, dict
, set
, bytearray
# ŠŃŃŃŠ¾ŠµŠ½Š½ŃŠµ ŠŗŠ¾Š½ŃŠµŠ¹Š½ŠµŃŃ:
c_str = "ŠŠ“Ń Š² Š¼Š°Š³Š°Š·ŠøŠ½ Š² Š³Š¾ŃŠ¾Š“Šµ Š”Š°Š½ŠŗŃ-ŠŠµŃŠµŃŠ±ŃŃŠ³Šµ"
c_tpl = (1, 1.2, "Ń")
c_rng = range(10)
c_fst = frozenset({1,2,3}) # readonly set
c_bts = bytes((3,1,4,5,1,5))
c_lst = [1,2,3]
c_dct = {1: "Sex", 2: "Drugs", 3: "Rock-and-Roll"}
c_set = {1,2,3}
c_bar = bytearray((3,1,4,5,1,5)) # writable bytes
Š ŃŠ°Š±Š»ŠøŃŠµ ŠæŃŠøŠ²ŠµŠ“ŠµŠ½Ń Š½ŠµŠŗŠ¾ŃŠ¾ŃŃŠµ ŠæŠ¾ŠæŃŠ»ŃŃŠ½ŃŠµ Š²ŃŃŃŠ¾ŠµŠ½Š½ŃŠµ ŠŗŠ¾Š½ŃŠµŠ¹Š½ŠµŃŃ Šø Š½ŠµŠŗŠ¾ŃŠ¾ŃŃŠµ Š¾ŠæŠµŃŠ°ŃŠøŠø Š½Š°Š“ Š½ŠøŠ¼Šø Ń Š°ŃŠøŠ¼ŠæŃŠ¾ŃŠøŃŠµŃŠŗŠ¾Š¹ ŃŠ»Š¾Š¶Š½Š¾ŃŃŃŃ.
ŠŠ±ŃŠµŠŗŃŃ Š² Python Š¼Š¾Š³ŃŃ Š±ŃŃŃ Ń
ŃŃŠøŃŃŃŠµŠ¼ŃŠµ āĀ ŃŠ°ŠŗŠøŠ¼ Š¾Š±ŃŠµŠŗŃŠ°Š¼ ŃŠ¾ŠæŠ¾ŃŃŠ°Š²Š»ŃŠµŃŃŃ Š½ŠµŠŗŠ¾ŃŠ¾ŃŠ¾Šµ ŃŠøŃŠ»Š¾ (Ń
ŃŃ), ŠŗŠ¾ŃŠ¾ŃŠ¾Šµ Š½Šµ Š¼ŠµŠ½ŃŠµŃŃŃ Š² ŃŠµŃŠµŠ½ŠøŠø ŃŃŃŠµŃŃŠ²Š¾Š²Š°Š½ŠøŃ Š¾Š±ŃŠµŠŗŃŠ°. Š„ŃŃŠø ŠøŃŠæŠ¾Š»ŃŠ·ŃŃŃŃŃ Š“Š»Ń ŃŃŠ°Š²Š½ŠµŠ½ŠøŃ ŠŗŠ»ŃŃŠµŠ¹ Š² ŃŠ»Š¾Š²Š°ŃŃŃ
Šø Š¼Š½Š¾Š¶ŠµŃŃŠ²Š°Ń
. Š¢ŠµŃ
Š½ŠøŃŠµŃŠŗŠø, Ń Ń
ŃŃŠøŃŃŠµŠ¼ŃŃ
Š¾Š±ŃŠµŠŗŃŠ¾Š² ŃŠµŠ°Š»ŠøŠ·Š¾Š²Š°Š½ Š¼ŠµŃŠ¾Š“ __hash__()
.
NB! ŠŃŠ»Šø ŠŗŠ¾Š½ŃŠµŠ¹Š½ŠµŃ Šø ŠµŠ³Š¾ ŃŠ»ŠµŠ¼ŠµŠ½ŃŃ Š½ŠµŠøŠ·Š¼ŠµŠ½ŃŠµŠ¼ŃŠµ, ŃŠ¾ Š¾Š½ Ń ŃŃŠøŃŃŠµŠ¼ŃŠ¹.
# Š„ŃŃŠøŃŃŠµŠ¼ŃŠ¹ ŠŗŠ¾ŃŃŠµŠ¶
hash((1,3,()))
# ŠŠµŃ
ŃŃŠøŃŃŠµŠ¼ŃŠ¹ ŠŗŠ¾ŃŃŠµŠ¶ (Š“Š¾Š»Š¶Š½Š° Š±ŃŃŃ Š¾ŃŠøŠ±ŠŗŠ°)
hash((1,3,[]))
ŠŠøŠ¶Šµ ŠæŃŠøŠ²ŠµŠ“ŠµŠ½Ń Š½ŠµŠŗŠ¾ŃŠ¾ŃŃŠµ ŠŗŠ¾Š½ŃŠµŠ¹Š½ŠµŃŃ ŠøŠ· ŃŃŠ°Š½Š“Š°ŃŃŠ½Š¾Š¹ Š±ŠøŠ±Š»ŠøŠ¾ŃŠµŠŗŠø ŃŠ¾ ŃŠæŠµŃŠøŠ°Š»ŃŠ½ŃŠ¼ Š½Š°Š·Š½Š°ŃŠµŠ½ŠøŠµŠ¼.
# ŠŃŠøŠ¼ŠµŃ ŠøŠ¼ŠµŠ½Š¾Š²Š°Š½Š½Š¾Š³Š¾ ŠŗŠ¾ŃŃŠµŠ¶Š°
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(1,2)
# Š ŠæŠ¾Š»ŃŠ¼ namedtuple Š¼Š¾Š¶Š½Š¾ Š¾Š±ŃŠ°ŃŠ°ŃŃŃŃ ŃŠµŃŠµŠ· ŃŠ¾ŃŠŗŃ.
p.x
# Š¢Š°ŠŗŠ¶Šµ Š¼Š¾Š¶Š½Š¾ ŃŠøŃŠ°ŃŃ ŠŗŠ°Šŗ Š¾Š±ŃŃŠ½ŃŠ¹ ŠŗŠ¾ŃŃŠµŠ¶.
p[0]
# ŠŃŠµŃŠøŃŠ¾Š²Š°Š½ŠøŠµ ŠæŠ¾ ŠøŠ¼ŠµŠ½Š¾Š²Š°Š½Š½Š¾Š¼Ń ŠŗŠ¾ŃŃŠµŠ¶Ń
for x in p:
print(x)
# ŠŠ¼ŠµŠ½Š¾Š²Š°Š½Š½ŃŠ¹ ŠŗŠ¾ŃŃŠµŠ¶ āĀ Š½ŠµŠøŠ·Š¼ŠµŠ½ŃŠµŠ¼ŃŠ¹ Š¾Š±ŃŠµŠŗŃ (Š“Š¾Š»Š¶Š½Š° Š±ŃŃŃ Š¾ŃŠøŠ±ŠŗŠ°)
p.x = 4
NB! namedtuple
ŃŠ“Š¾Š±Š½Š¾ ŠøŃŠæŠ¾Š»ŃŠ·Š¾Š²Š°ŃŃ Š“Š»Ń Š²Š¾Š·Š²ŃŠ°ŃŠ° Š“Š°Š½Š½ŃŃ
ŠøŠ· ŃŃŠ½ŠŗŃŠøŠ¹.
Update: Š Python 3.7 ŃŠ¶Šµ Š½Šµ Š°ŠŗŃŃŠ°Š»ŃŠ½Š¾. Š”ŃŠ°Š½Š“Š°ŃŃŠ½ŃŠ¹ dict
Š³Š°ŃŠ°Š½ŃŠøŃŃŠµŃ ŠæŠ¾ŃŃŠ“Š¾Šŗ.
dict
Š½Šµ Š³Š°ŃŠ°Š½ŃŠøŃŃŠµŃ, ŃŃŠ¾ Š±ŃŠ“ŠµŃ Ń
ŃŠ°Š½ŠøŃŃ ŠŗŠ»ŃŃŠø Š² ŃŠ¾Š¼ ŠæŠ¾ŃŃŠ“ŠŗŠµ, Š² ŠŗŠ¾ŃŠ¾ŃŠ¾Š¼ Š²Ń ŠøŃ
Š·Š°Š“Š°Š»Šø. ŠŃŠ»Šø Š²Š°Š¶Š½Š¾ ŠæŠ¾Š»ŃŃŠøŃŃ ŠøŠ· d.keys()
Š¾ŃŠøŠ³ŠøŠ½Š°Š»ŃŠ½ŃŠ¹ ŠæŠ¾ŃŃŠ“Š¾Šŗ, ŃŠ¾ Š“Š»Ń ŃŃŠ¾Š³Š¾ ŠµŃŃŃ OrderedDict
.
# ŠŠæŠµŃŠ°ŃŠøŃ ŃŠµŠ²ŠµŃŃŠ° Š½Š° ŃŠ»Š¾Š²Š°ŃŠµ Š±ŠµŃŠ¼ŃŃŠ»ŠµŠ½Š½Š° ā ŠæŠ¾ŃŃŠ“ŠŗŠ° Š½ŠµŃ (Š“Š¾Š»Š¶Š½Š° Š±ŃŃŃ Š¾ŃŠøŠ±ŠŗŠ°)
dict_std = {"a": 1, "b": 2, "c": 3}
[k for k in reversed(dict_std)]
# Š Š°Š±Š¾ŃŠøŠ¹ workaround Š² Python 3.7+
dict_std = {"a": 1, "b": 2, "c": 3}
[k for k in reversed(list(dict_std.keys()))]
# OrderedDict ŠæŃŠµŠ“ŠæŠ¾Š»Š°Š³Š°ŠµŃ Š½Š°Š»ŠøŃŠøŠµ ŠæŠ¾ŃŃŠ“ŠŗŠ°, Š¼Š¾Š¶Š½Š¾ ŠæŠµŃŠµŠ²ŠµŃŠ½ŃŃŃ
from collections import OrderedDict
dict_odr = OrderedDict({"a": 1, "b": 2, "c": 3})
[k for k in reversed(dict_odr)]
ŠŃŠ»Šø Š¾Š±ŃŠ°ŃŠøŃŃŃŃ Šŗ ŠŗŠ»ŃŃŃ ŃŠ»Š¾Š²Š°ŃŃ, ŠŗŠ¾ŃŠ¾ŃŠ¾Š³Š¾ Š½ŠµŃ, ŃŠ¾ Š±ŃŠ“ŠµŃ Š¾ŃŠøŠ±ŠŗŠ°.
# ŠŠ±ŃŃŠ½ŃŠ¹ ŃŠ»Š¾Š²Š°ŃŃ ŃŃŠ³Š°ŠµŃŃŃ Š½Š° ŠæŠ¾Š»Ń, ŠŗŠ¾ŃŠ¾ŃŃŃ
Š½ŠµŃ (Š“Š¾Š»Š¶Š½Š° Š±ŃŃŃ Š¾ŃŠøŠ±ŠŗŠ°)
d = {}
d["4"]
ŠŃŠ»Šø Š½Š°Š“Š¾ ŠŗŠ°ŠŗŠ¾Šµ-ŃŠ¾ ŠæŠ¾Š²ŠµŠ“ŠµŠ½ŠøŠµ ŠæŠ¾-ŃŠ¼Š¾Š»ŃŠ°Š½ŠøŃ, ŃŠ¾ Š“Š»Ń ŃŃŠ¾Š³Š¾ ŃŠ“Š¾Š±Š½Š¾ ŠøŃŠæŠ¾Š»ŃŠ·Š¾Š²Š°ŃŃ defaultdict
.
# ŠŃŠøŠ¼ŠµŃ Ń defaultdict, ŠŗŠ»ŃŃŃ ŠæŠ¾-ŃŠ¼Š¾Š»ŃŠ°Š½ŠøŃ ŠæŃŠøŃŠ²Š°ŠµŃŃŃ 0.
from collections import defaultdict
s = 'Ń Š·Š°Š»ŠøŠ²Š°Ń Š³Š»Š°Š·Š° ŠŗŠµŃŠ¾ŃŠøŠ½Š¾Š¼'
d = defaultdict(int)
for k in s:
d[k] += 1 # Š±ŠµŠ· defaultdict Š·Š“ŠµŃŃ Š½Š°Š“Š¾ Š±ŃŠ»Š¾ Š±Ń ŠøŠ½ŠøŃŠøŠ°Š»ŠøŠ·ŠøŃŠ¾Š²Š°ŃŃ ŠŗŠ»ŃŃŠø ŃŠ²Š½Š¾
sorted(d.items(), key=lambda x: x[1], reverse=True)
ChainMap
Ń
ŃŠ°Š½ŠøŃ ŃŃŃŠ»ŠŗŠø Š½Š° Š“ŃŃŠ³ŠøŠµ ŃŠ»Š¾Š²Š°ŃŠø. ŠŠ¾Š½ŃŠµŠ¹Š½ŠµŃ ŠæŠ¾Š·Š²Š¾Š»ŃŠµŃ ŃŠ°Š±Š¾ŃŠ°ŃŃ Ń Š²Š½ŃŃŃŠµŠ½Š½ŠøŠ¼Šø ŃŠ»Š¾Š²Š°ŃŃŠ¼Šø ŠŗŠ°Šŗ Ń Š¾Š“Š½ŠøŠ¼ ŃŠµŠ»ŃŠ¼ ŃŠ»Š¾Š²Š°ŃŠµŠ¼.
Š£ ChainMap
ŠµŃŃŃ Š“Š²Š° ŃŃŠ°Š“ŠøŃŠøŠ¾Š½Š½ŃŃ
ŠæŃŠøŠ»Š¾Š¶ŠµŠ½ŠøŃ:
# ŠŃŠøŠ¼ŠµŃ Ń ChainMap: ŠæŠ¾ŠøŃŠŗ ŠæŠ¾ Š“Š²ŃŠ¼ ŃŠ»Š¾Š²Š°ŃŃŠ¼
from collections import ChainMap
group1 = {"a": 1, "b": 2}
group2 = {"c": 3, "d": 4}
groups = ChainMap(group1, group2)
groups["d"], groups["b"]
ŠŠ“ŠµŃŃ group1
Š¼Š¾Š¶ŠµŃ Š±ŃŃŃ ŃŠ»Š¾Š²Š°ŃŃŠ¼ ŠæŠ°ŃŠ°Š¼ŠµŃŃŠ¾Š², Š° group2
ā ŃŠ»Š¾Š²Š°ŃŃŠ¼ Š·Š½Š°ŃŠµŠ½ŠøŠ¹ ŠæŠ°ŃŠ°Š¼ŠµŃŃŠ¾Š² ŠæŠ¾-ŃŠ¼Š¾Š»ŃŠ°Š½ŠøŃ. ŠŃŠ»Šø Š·Š½Š°ŃŠµŠ½ŠøŃ Š½ŠµŃ Š² group1
, ŃŠ¾ Š²ŠµŃŠ½ŃŃŃŃ Š·Š½Š°ŃŠµŠ½ŠøŠµ ŠøŠ· group2
.
# ŠŃŠøŠ¼ŠµŃ ŃŠ»Š¾Š²Š°ŃŃ, Š² ŠŗŠ¾ŃŠ¾ŃŃŠ¹ Š½ŠµŠ»ŃŠ·Ń ŠæŠøŃŠ°ŃŃ
from types import MappingProxyType
d = {"a": 1, "b": 2} # ŠŠ±ŃŃŠ½ŃŠ¹ ŃŠ»Š¾Š²Š°ŃŃ
d_ro = MappingProxyType(d) # MappingProxyType (Read Only)
d_ro["a"]
# ŠŠ°ŠæŠøŃŠ°ŃŃ Š² MappingProxyType Š½ŠµŠ»ŃŠ·Ń (Š“Š¾Š»Š¶Š½Š° Š±ŃŃŃ Š¾ŃŠøŠ±ŠŗŠ°)
d_ro["a"] = 1
ŠŃŠ»ŃŃŠøŠ¼Š½Š¾Š¶ŠµŃŃŠ²Š¾ āĀ Š¼Š½Š¾Š¶ŠµŃŃŠ²Š¾, ŃŠ»ŠµŠ¼ŠµŠ½ŃŃ Š² ŠŗŠ¾ŃŠ¾ŃŠ¾Š¼ Š¼Š¾Š³ŃŃ ŠæŠ¾Š²ŃŠ¾ŃŃŃŃŃŃ.
# ŠŃŠøŠ¼ŠµŃ ŠæŠ¾ŃŃŃŠ¾ŠµŠ½ŠøŃ Š¼ŃŠ»ŃŃŠøŠ¼Š½Š¾Š¶ŠµŃŃŠ²Š° ŠæŠ¾ ŠøŃŠµŃŠøŃŃŠµŠ¼Š¾Š¼Ń Š¾Š±ŃŠµŠŗŃŃ (ŃŃŃŠ¾ŠŗŠµ)
from collections import Counter
s = 'Ń Š·Š°Š»ŠøŠ²Š°Ń Š³Š»Š°Š·Š° ŠŗŠµŃŠ¾ŃŠøŠ½Š¾Š¼'
c = Counter(s)
c
# sum āĀ ŠŗŠ¾Š»ŠøŃŠµŃŃŠ²Š¾ ŃŠ»ŠµŠ¼ŠµŠ½ŃŠ¾Š² Š² Š¼Š½Š¾Š¶ŠµŃŃŠ²Šµ,
# len - ŠŗŠ¾Š»ŠøŃŠµŃŃŠ²Š¾ ŃŠ½ŠøŠŗŠ°Š»ŃŠ½ŃŃ
ŃŠ»ŠµŠ¼ŠµŠ½ŃŠ¾Š² Š² Š¼Š½Š¾Š¶ŠµŃŃŠ²Šµ
sum(c.values()), len(c.values())
Š Š¾ŃŠµŃŠµŠ“Ń Šø ŃŃŠµŠŗ Š¼Š¾Š¶Š½Š¾ ŃŠµŠ°Š»ŠøŠ·Š¾Š²Š°ŃŃ Š½Š° list
. ŠŠ¾ list
ŃŠµŠ°Š»ŠøŠ·Š¾Š²Š°Š½ Š½Š° Š“ŠøŠ½Š°Š¼ŠøŃŠµŃŠŗŠ¾Š¼ Š¼Š°ŃŃŠøŠ²Šµ, Š° deque
Š½Š° ŃŠ²ŃŠ·Š½Š¾Š¼ ŃŠæŠøŃŠŗŠµ.
# ŠŃŠøŠ¼ŠµŃ Š“Š²ŃŃŃŠ¾ŃŠ¾Š½Š½ŠµŠ¹ Š¾ŃŠµŃŠµŠ“Šø
from collections import deque
d = deque()
d.append("Ń")
d.append("ŠøŠ“Ń")
d.append("Š±ŃŠ°ŃŃ")
d.append("Š»ŃŃ")
d.popleft(), d.pop()
# ŠŃŠøŠ¼ŠµŃ
from heapq import heappush, heappop
q = []
heappush(q, (3, "ŠŃŠ¾Š“Š°ŠŗŃŠµŠ½"))
heappush(q, (1, "Š§ŠæŠ¾Šŗ"))
heappush(q, (2, "Š§ŠæŠ¾Šŗ"))
heappop(q),heappop(q),heappop(q)
ŠŠ¾Š³Š“Š° Š½ŃŠ¶ŠµŠ½ ŠøŠ·Š¼ŠµŠ½ŃŠµŠ¼ŃŠ¹ namedtuple
, ŃŠ¾ ŠæŠ¾Š“Ń
Š¾Š“ŠøŃ dataclasses
.
# ŠŃŠøŠ¼ŠµŃ Ń ŃŠ¾Š±ŃŃŠ²ŠµŠ½Š½ŃŠ¼ dataclass
from dataclasses import dataclass
@dataclass
class Structure:
name: str
value: float
@property
def square(self) -> float:
return self.value * self.value
s = Structure("Ń", 2)
s.square
ŠŠ¾-ŃŠ¼Š¾Š»ŃŠ°Š½ŠøŃ dataclass
Š³ŠµŠ½ŠµŃŠøŃŃŠµŃ 3 Š“Š°Š½Š“ŠµŃ-Š¼ŠµŃŠ¾Š“Š° (Š¼ŠµŃŠ¾Š“ Ń Š“Š²Š¾Š¹Š½ŃŠ¼ ŠæŠ¾Š“ŃŠµŃŠŗŠøŠ²Š°Š½ŠøŠµŠ¼):
init
,repr
,eq
.Š§ŠµŃŠµŠ· Š¾ŠæŃŠøŠø Š“ŠµŠŗŠ¾ŃŠ°ŃŠ¾ŃŠ° Š¼Š¾Š¶Š½Š¾ ŃŠ“ŠµŠ»Š°ŃŃ dataclass
immutable (frozen
), mutable-Ń
ŃŃŠøŃŃŠµŠ¼ŃŠ¼ (unsafe_hash
) Šø ŃŃŠ°Š²Š½ŠøŠ²Š°ŠµŠ¼ŃŠ¼ (order
).
%load_ext watermark
%watermark -d -u -v -iv