ŠšŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Ń‹ 1Ā¶

1. Šž ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Š°Ń…Ā¶

ŠšŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€ ā€” Š¾Š±ŃŠŠµŠŗт, ŠŗŠ¾Ń‚Š¾Ń€Ń‹Š¹ сŠ¾Š“ŠµŃ€Š¶Šøт Š²Š½ŃƒŃ‚Ń€Šø сŠµŠ±Ń Š“руŠ³ŠøŠµ Š¾Š±ŃŠŠµŠŗты. Š¢ŠµŃ…Š½ŠøчŠµŃŠŗŠø Š²ŃŠµ ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Ń‹ Š½Š°ŃŠ»ŠµŠ“уют Š¾Ń‚ Collections.abc.Container Š¼ŠµŃ‚Š¾Š“ __contains__. Š” этŠøŠ¼ Š¼ŠµŃ‚Š¾Š“Š¾Š¼ рŠ°Š±Š¾Ń‚Š°ŠµŃ‚ Š¾ŠæŠµŃ€Š°Ń‚Š¾Ń€ in.

ŠšŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Ń‹ ŠæрŠµŠ“стŠ°Š²Š»ŠµŠ½Ń‹ рŠ°Š·Š½Ń‹Š¼Šø струŠŗтурŠ°Š¼Šø Š“Š°Š½Š½Ń‹Ń…. ŠŠ° этŠøх струŠŗтурŠ°Ń… Š¼Š¾Š¶Š½Š¾ ŠæŠ¾ŃŃ‚Ń€Š¾Šøть Š²ŃŠµŠ²Š¾Š·Š¼Š¾Š¶Š½Ń‹Šµ Š°Š±ŃŃ‚Ń€Š°ŠŗтыŠµ тŠøŠæы Š“Š°Š½Š½Ń‹Ń….

NB! ŠŠµ Š½Š°Š“Š¾ ŠæутŠ°Ń‚ŃŒ тŠøŠæы Š“Š°Š½Š½Ń‹Ń… сŠ¾ струŠŗтурŠ°Š¼Šø Š“Š°Š½Š½Ń‹Ń… Šø Š°Š±ŃŃ‚Ń€Š°ŠŗтŠ½Ń‹Š¼Šø тŠøŠæŠ°Š¼Šø Š“Š°Š½Š½Ń‹Ń….

Š¢ŠøŠæ Š“Š°Š½Š½Ń‹Ń… ā€”Ā Ń…Š°Ń€Š°ŠŗтŠµŃ€ŠøстŠøŠŗŠ° ŠŗусŠ¾Ń‡ŠŗŠ° Š“Š°Š½Š½Ń‹Ń…, ŠŗŠ¾Ń‚Š¾Ń€Š°Ń Š¾ŠæрŠµŠ“ŠµŠ»ŃŠµŃ‚ сŠæŠ¾ŃŠ¾Š± ŠµŠ³Š¾ ŠøŠ½Ń‚ŠµŃ€ŠæрŠµŃ‚Š°Ń†ŠøŠø. ŠŠ°ŠæрŠøŠ¼ŠµŃ€, b1100001 Š¼Š¾Š¶ŠµŃ‚ Š±Ń‹Ń‚ŃŒ ŠŗŠ°Šŗ чŠøсŠ»Š¾ 97 (цŠµŠ»Ń‹Š¹ тŠøŠæ), Š° Š¼Š¾Š¶ŠµŃ‚ Š±Ń‹Ń‚ŃŒ ŠŗŠ°Šŗ сŠøŠ¼Š²Š¾Š» a ŠøŠ· ASCII (сŠøŠ¼Š²Š¾Š»).

Š”труŠŗтурŠ° Š“Š°Š½Š½Ń‹Ń… ā€”Ā Š½Š°Š±Š¾Ń€ ŠøŠ· ŠæрŠøŠ¼ŠøтŠøŠ²Š¾Š² Š“Š°Š½Š½Ń‹Ń… Šø Š¾ŠæŠµŃ€Š°Ń†ŠøŠ¹ Š½Š°Š“ Š½ŠøŠ¼Šø, Š¾Š³Ń€Š°Š½ŠøŠ·Š¾Š²Š°Š½Š½Ń‹Šµ Š“Š»Ń эффŠµŠŗтŠøŠ²Š½Š¾Š³Š¾ рŠµŃˆŠµŠ½Šøя Š·Š°Š“Š°Ń‡Šø. ŠŠ°ŠæрŠøŠ¼ŠµŃ€, Š¼Š°ŃŃŠøŠ²) ŠøŠ»Šø сŠ²ŃŠ·Š½Ń‹Š¹ сŠæŠøсŠ¾Šŗ.

ŠŠ±ŃŃ‚Ń€Š°ŠŗтŠ½Ń‹Š¹ тŠøŠæ Š“Š°Š½Š½Ń‹Ń… (ŠŠ¢Š”) ā€”Ā Š¼Š°Ń‚ŠµŠ¼Š°Ń‚ŠøчŠµŃŠŗŠ°Ń Š¼Š¾Š“ŠµŠ»ŃŒ струŠŗтуры Š“Š°Š½Š½Ń‹Ń…, ŠµŠµ ŠøŠ½Ń‚ŠµŃ€Ń„ŠµŠ¹Ń. ŠŠ°ŠæрŠøŠ¼ŠµŃ€, Š¾Ń‡ŠµŃ€ŠµŠ“ь ā€” Š°Š±ŃŃ‚Ń€Š°ŠŗтŠ½Ń‹Š¹ тŠøŠæ Š“Š°Š½Š½Ń‹Ń…, ŠŗŠ¾Ń‚Š¾Ń€Ń‹Š¹ Š¼Š¾Š¶Š½Š¾ рŠµŠ°Š»ŠøŠ·Š¾Š²Š°Ń‚ŃŒ Š½Š° рŠ°Š·Š½Ń‹Ń… струŠŗтурŠ°Ń… Š“Š°Š½Š½Ń‹Ń….

ŠŸŃ€Š¾Š±Š»ŠµŠ¼Š° ŠŗŠ¾ŠæŠøрŠ¾Š²Š°Š½Šøя ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Š¾Š²Ā¶

Š’ Python ŠæрŠøсŠ²Š°ŠøŠ²Š°Š½ŠøŠµ = ŠŗŠ¾ŠæŠøруŠµŃ‚ тŠ¾Š»ŃŒŠŗŠ¾ ссыŠ»ŠŗŠø Š¾Š±ŃŠŠµŠŗтŠ¾Š². Š˜ Š¾ŠæрŠµŠ°Ń†Šøю Š²ŠøŠ“Š° name = obj Š¼Š¾Š¶Š½Š¾ ŠøŠ½Ń‚ŠµŃ€ŠæрŠµŃ‚ŠøрŠ¾Š²Š°Ń‚ŃŒ ŠŗŠ°Šŗ ŠæрŠøсŠ²Š°ŠøŠ²Š°Š½ŠøŠµ Š¾Š±ŃŠŠµŠŗту obj ŠøŠ¼ŠµŠ½Šø name. ŠšŠ°Šŗ Š² тŠ°ŠŗŠ¾Š¼ сŠ»ŃƒŃ‡Š°Šµ сŠŗŠ¾ŠæŠøрŠ¾Š²Š°Ń‚ŃŒ Š¾Š±ŃŠŠµŠŗт?

Š”Š»Ń ŠæрŠøŠ¼ŠµŃ€Š°, Š“Š°Š²Š°Š¹Ń‚Šµ ŠæŠ¾ŠæрŠ¾Š±ŃƒŠµŠ¼ сŠŗŠ¾ŠæŠøрŠ¾Š²Š°Ń‚ŃŒ сŠæŠøсŠ¾Šŗ сŠæŠøсŠŗŠ¾Š².

InĀ [1]:
# ŠŸŃ€ŠøŠ¼ŠµŃ€ сŠæŠøсŠŗŠ° сŠæŠøсŠŗŠ¾Š²
a = [1,2,3]
b = [4,5,6]
c = [a,b] # сŠ¾Š“ŠµŃ€Š¶Šøт ссыŠ»ŠŗŠø Š½Š° a Šø b

Š•ŃŠ»Šø Š¼Ń‹ сŠæŠøсŠ¾Šŗ c ŠæрŠøсŠ²Š¾ŠøŠ¼ сŠæŠøсŠŗу d, тŠ¾ ŠæŠ¾ фŠ°Šŗту у сŠæŠøсŠŗŠ° [a,b] ŠæрŠ¾ŃŃ‚Š¾ Š±ŃƒŠ“ŠµŃ‚ Š“Š²Š° ŠøŠ¼ŠµŠ½ŠøŠµ c Šø d.

InĀ [2]:
# ŠŠ°Š²ŠµŃˆŠøŠ²Š°ŠµŠ¼ Š½Š¾Š²Š¾Šµ ŠøŠ¼Ń сŠæŠøсŠŗу с (у Š¾Š±ŠµŃŠŠµŠŗтŠ¾Š² Š“Š°Š¾Š»Š¶Š½Ń‹ Š±Ń‹Ń‚ŃŒ Š¾Š“ŠøŠ½Š°ŠŗŠ¾Š²Ń‹Šµ id)
d = c
id(c), id(d)
Out[2]:
(4508213760, 4508213760)

ŠœŠ¾Š¶Š½Š¾ сŠŗŠ¾ŠæŠøрŠ¾Š²Š°Ń‚ŃŒ сŠæŠøсŠ¾Šŗ, сŠ¾Š·Š“Š°Š² яŠ²Š½Š¾ Š½Š¾Š²Ń‹Š¹ с сŠ¾Š“ŠµŃ€Š¶ŠøŠ¼Ń‹Š¼ стŠ°Ń€Š¾Š³Š¾. ŠŸŃ€Šø этŠ¾Š¼ ŠæрŠ¾ŠøсхŠ¾Š“Šøт ŠæŠ¾Š²ŠµŃ€Ń…Š½Š¾ŃŃ‚Š½Š¾Šµ ŠŗŠ¾ŠæŠøрŠ¾Š²Š°Š½ŠøŠµ сŠæŠøсŠŗŠ°. Š­Š»ŠµŠ¼ŠµŠ½Ń‚Ń‹ уŠŗŠ°Š·Ń‹Š²Š°ŃŽŃ‚ Š½Š° эŠ»ŠµŠ¼ŠµŠ½Ń‚Ń‹ стŠ°Ń€Š¾Š³Š¾ сŠæŠøсŠŗŠ°.

InĀ [3]:
# ŠŸŠ¾Š²ŠµŃ€Ń…Š½Š¾ŃŃ‚Š½Š¾Šµ ŠŗŠ¾ŠæŠøрŠ¾Š²Š°Š½ŠøŠµ сŠæŠøсŠŗŠ° (сŠæŠøсŠŗŠø рŠ°Š·Š½Ń‹Šµ, Š½Š¾ Š²Š½ŃƒŃ‚Ń€ŠµŠ½Š½ŠøŠµ Š¾Š±ŃŠŠµŠŗты Š½ŠµŃ‚)
d = list(c)
id(c), id(d), id(c[0]), id(d[0])
Out[3]:
(4508213760, 4508896768, 4507249728, 4507249728)

Š Š°Š·Š»ŠøчŠ°ŃŽŃ‚ Š“Š²Š° тŠøŠæŠ° ŠŗŠ¾ŠæŠøрŠ¾Š²Š°Š½Šøя:

  • ŠŸŠ¾Š²ŠµŃ€Ń…Š½Š¾ŃŃ‚Š½Š¾Šµ (Š°Š½Š³Š». shallow)
  • Š“Š»ŃƒŠ±Š¾ŠŗŠ¾Šµ (Š°Š½Š³Š». deep)

Š’ Š¾Ń‚Š»ŠøчŠøŠø Š¾Ń‚ ŠæŠ¾Š²ŠµŃ€Ń…Š½Š¾ŃŃ‚Š½Š¾Š³Š¾ ŠŗŠ¾ŠæŠøрŠ¾Š²Š°Š½Šøя, Š³Š»ŃƒŠ±Š¾ŠŗŠ¾Šµ Š¾Š±Ń…Š¾Š“Šøт Š¾Š±ŃŠŠµŠŗт рŠµŠŗурсŠøŠ²Š½Š¾ Šø ŠŗŠ¾ŠæŠøруŠµŃ‚ Š²ŃŠµ ŠæŠ¾Š“Š¾Š±ŃŠŠµŠŗты.

InĀ [4]:
# Š“Š»ŃƒŠ±Š¾ŠŗŠ¾Šµ ŠŗŠ¾ŠæŠøрŠ¾Š²Š°Š½ŠøŠµ сŠæŠøсŠŗŠ°
from copy import deepcopy
d = deepcopy(c)
id(c), id(d), id(c[0]), id(d[0])
Out[4]:
(4508213760, 4507242880, 4507249728, 4508854144)

2. Built-in ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Ń‹Ā¶

Š’стрŠ¾ŠµŠ½Š½Ń‹Šµ Š² Python-ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Ń‹ Š¼Š¾Š¶Š½Š¾ рŠ°Š·Š“ŠµŠ»Šøть Š½Š° Š“Š²Šµ ŠŗŠ°Ń‚ŠµŠ³Š¾Ń€ŠøŠø:

  • Š½ŠµŠøŠ·Š¼ŠµŠ½ŃŠµŠ¼Ń‹Šµ (Š°Š½Š³Š». immutable) string, tuple, range, frozenset, bytes
  • ŠøŠ·Š¼ŠµŠ½ŃŠµŠ¼Ń‹Šµ (Š°Š½Š³Š». mutable) list, dict, set, bytearray
InĀ [5]:
# Š’стрŠ¾ŠµŠ½Š½Ń‹Šµ ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Ń‹:
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

Šž сŠ»Š¾Š¶Š½Š¾ŃŃ‚Šø Š¾ŠæŠµŃ€Š°Ń†ŠøŠ¹ Š½Š°Š“ ŠæŠ¾ŠæуŠ»ŃŃ€Š½Ń‹Š¼Šø ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Š°Š¼ŠøĀ¶

Š’ тŠ°Š±Š»ŠøцŠµ ŠæрŠøŠ²ŠµŠ“ŠµŠ½Ń‹ Š½ŠµŠŗŠ¾Ń‚Š¾Ń€Ń‹Šµ ŠæŠ¾ŠæуŠ»ŃŃ€Š½Ń‹Šµ Š²ŃŃ‚Ń€Š¾ŠµŠ½Š½Ń‹Šµ ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Ń‹ Šø Š½ŠµŠŗŠ¾Ń‚Š¾Ń€Ń‹Šµ Š¾ŠæŠµŃ€Š°Ń†ŠøŠø Š½Š°Š“ Š½ŠøŠ¼Šø с Š°ŃŠøŠ¼ŠæтŠ¾Ń‚ŠøчŠµŃŠŗŠ¾Š¹ сŠ»Š¾Š¶Š½Š¾ŃŃ‚ŃŒŃŽ.

ŠšŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€ List Tuple Dictionary Set
ŠŸŃƒŃŃ‚Š¾Š¹ ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€ [] $O(1)$ () $O(1)$ {} $O(1)$ Set() ŠøŠ»Šø {()} $O(1)$
ŠŸŃ€Š¾Ń‡ŠøтŠ°Ń‚ŃŒ эŠ»ŠµŠ¼ŠµŠ½Ń‚ l[i] $O(1)$ t[i] $O(1)$ d[key] $O(1)$ undefined undefined
Š”Š¾Š±Š°Š²Šøть эŠ»ŠµŠ¼ŠµŠ½Ń‚ l.append(5) $O(1)$ undefined undefined d[key]=5 $O(1)$ s.add(5) $O(1)$
Š£Š“Š°Š»Šøть эŠ»ŠµŠ¼ŠµŠ½Ń‚ del l[i] $O(N)$ undefined undefined del d[key] $O(1)$ s.discard(5) $O(1)$

Šž хэшŠøруŠµŠ¼Š¾ŃŃ‚ŠøĀ¶

ŠžŠ±ŃŠŠµŠŗты Š² Python Š¼Š¾Š³ŃƒŃ‚ Š±Ń‹Ń‚ŃŒ хэшŠøуруŠµŠ¼Ń‹Šµ ā€”Ā Ń‚Š°ŠŗŠøŠ¼ Š¾Š±ŃŠŠµŠŗтŠ°Š¼ сŠ¾ŠæŠ¾ŃŃ‚Š°Š²Š»ŃŠµŃ‚ся Š½ŠµŠŗŠ¾Ń‚Š¾Ń€Š¾Šµ чŠøсŠ»Š¾ (хэш), ŠŗŠ¾Ń‚Š¾Ń€Š¾Šµ Š½Šµ Š¼ŠµŠ½ŃŠµŃ‚ся Š² тŠµŃ‡ŠµŠ½ŠøŠø сущŠµŃŃ‚Š²Š¾Š²Š°Š½Šøя Š¾Š±ŃŠŠµŠŗтŠ°. Š„эшŠø ŠøсŠæŠ¾Š»ŃŒŠ·ŃƒŃŽŃ‚ся Š“Š»Ń срŠ°Š²Š½ŠµŠ½Šøя ŠŗŠ»ŃŽŃ‡ŠµŠ¹ Š² сŠ»Š¾Š²Š°Ń€ŃŃ… Šø Š¼Š½Š¾Š¶ŠµŃŃ‚Š²Š°Ń…. Š¢ŠµŃ…Š½ŠøчŠµŃŠŗŠø, у хэшŠøруŠµŠ¼Ń‹Ń… Š¾Š±ŃŠŠµŠŗтŠ¾Š² рŠµŠ°Š»ŠøŠ·Š¾Š²Š°Š½ Š¼ŠµŃ‚Š¾Š“ __hash__().

NB! Š•ŃŠ»Šø ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€ Šø ŠµŠ³Š¾ эŠ»ŠµŠ¼ŠµŠ½Ń‚Ń‹ Š½ŠµŠøŠ·Š¼ŠµŠ½ŃŠµŠ¼Ń‹Šµ, тŠ¾ Š¾Š½ хэшŠøруŠµŠ¼Ń‹Š¹.

InĀ [6]:
# Š„эшŠøруŠµŠ¼Ń‹Š¹ ŠŗŠ¾Ń€Ń‚ŠµŠ¶
hash((1,3,()))
Out[6]:
7764519317587398582
InĀ [7]:
# ŠŠµŃ…ŃŃˆŠøруŠµŠ¼Ń‹Š¹ ŠŗŠ¾Ń€Ń‚ŠµŠ¶ (Š“Š¾Š»Š¶Š½Š° Š±Ń‹Ń‚ŃŒ Š¾ŃˆŠøŠ±ŠŗŠ°)
hash((1,3,[]))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-cbf3b29f821f> in <module>
      1 # ŠŠµŃ…ŃŃˆŠøруŠµŠ¼Ń‹Š¹ ŠŗŠ¾Ń€Ń‚ŠµŠ¶ (Š“Š¾Š»Š¶Š½Š° Š±Ń‹Ń‚ŃŒ Š¾ŃˆŠøŠ±ŠŗŠ°)
----> 2 hash((1,3,[]))

TypeError: unhashable type: 'list'

3. ŠšŃ€ŃƒŃ‚Ń‹Šµ ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Ń‹Ā¶

ŠŠøŠ¶Šµ ŠæрŠøŠ²ŠµŠ“ŠµŠ½Ń‹ Š½ŠµŠŗŠ¾Ń‚Š¾Ń€Ń‹Šµ ŠŗŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€Ń‹ ŠøŠ· стŠ°Š½Š“Š°Ń€Ń‚Š½Š¾Š¹ Š±ŠøŠ±Š»ŠøŠ¾Ń‚ŠµŠŗŠø сŠ¾ сŠæŠµŃ†ŠøŠ°Š»ŃŒŠ½Ń‹Š¼ Š½Š°Š·Š½Š°Ń‡ŠµŠ½ŠøŠµŠ¼.

namedtuple ā€”Ā ŠŗŠ¾Š³Š“Š° Š½ŃƒŠ¶ŠµŠ½ Ā«ŃŠøшŠ½Ń‹Š¹ structĀ»Ā¶

InĀ [8]:
# ŠŸŃ€ŠøŠ¼ŠµŃ€ ŠøŠ¼ŠµŠ½Š¾Š²Š°Š½Š½Š¾Š³Š¾ ŠŗŠ¾Ń€Ń‚ŠµŠ¶Š°
from collections import namedtuple 
Point = namedtuple('Point', ['x', 'y'])
p = Point(1,2)
InĀ [9]:
# Šš ŠæŠ¾Š»ŃŠ¼ namedtuple Š¼Š¾Š¶Š½Š¾ Š¾Š±Ń€Š°Ń‰Š°Ń‚ŃŒŃŃ чŠµŃ€ŠµŠ· тŠ¾Ń‡Šŗу.
p.x
Out[9]:
1
InĀ [10]:
# Š¢Š°ŠŗŠ¶Šµ Š¼Š¾Š¶Š½Š¾ чŠøтŠ°Ń‚ŃŒ ŠŗŠ°Šŗ Š¾Š±Ń‹Ń‡Š½Ń‹Š¹ ŠŗŠ¾Ń€Ń‚ŠµŠ¶.
p[0]
Out[10]:
1
InĀ [11]:
# Š˜Ń‚ŠµŃ€ŠøрŠ¾Š²Š°Š½ŠøŠµ ŠæŠ¾ ŠøŠ¼ŠµŠ½Š¾Š²Š°Š½Š½Š¾Š¼Ńƒ ŠŗŠ¾Ń€Ń‚ŠµŠ¶Ńƒ
for x in p:
    print(x)
1
2
InĀ [12]:
# Š˜Š¼ŠµŠ½Š¾Š²Š°Š½Š½Ń‹Š¹ ŠŗŠ¾Ń€Ń‚ŠµŠ¶ ā€”Ā Š½ŠµŠøŠ·Š¼ŠµŠ½ŃŠµŠ¼Ń‹Š¹ Š¾Š±ŃŠŠµŠŗт (Š“Š¾Š»Š¶Š½Š° Š±Ń‹Ń‚ŃŒ Š¾ŃˆŠøŠ±ŠŗŠ°)
p.x = 4
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-4ffc5fe135f6> in <module>
      1 # Š˜Š¼ŠµŠ½Š¾Š²Š°Š½Š½Ń‹Š¹ ŠŗŠ¾Ń€Ń‚ŠµŠ¶ ā€”Ā Š½ŠµŠøŠ·Š¼ŠµŠ½ŃŠµŠ¼Ń‹Š¹ Š¾Š±ŃŠŠµŠŗт (Š“Š¾Š»Š¶Š½Š° Š±Ń‹Ń‚ŃŒ Š¾ŃˆŠøŠ±ŠŗŠ°)
----> 2 p.x = 4

AttributeError: can't set attribute

NB! namedtuple уŠ“Š¾Š±Š½Š¾ ŠøсŠæŠ¾Š»ŃŒŠ·Š¾Š²Š°Ń‚ŃŒ Š“Š»Ń Š²Š¾Š·Š²Ń€Š°Ń‚Š° Š“Š°Š½Š½Ń‹Ń… ŠøŠ· фуŠ½ŠŗцŠøŠ¹.

OrderedDict ā€”Ā ŠŗŠ¾Š³Š“Š° Š²Š°Š¶ŠµŠ½ ŠæŠ¾Ń€ŃŠ“Š¾Šŗ Š² сŠ»Š¾Š²Š°Ń€ŠµĀ¶

Update: Š’ Python 3.7 уŠ¶Šµ Š½Šµ Š°ŠŗтуŠ°Š»ŃŒŠ½Š¾. Š”тŠ°Š½Š“Š°Ń€Ń‚Š½Ń‹Š¹ dict Š³Š°Ń€Š°Š½Ń‚ŠøруŠµŃ‚ ŠæŠ¾Ń€ŃŠ“Š¾Šŗ.

dict Š½Šµ Š³Š°Ń€Š°Š½Ń‚ŠøруŠµŃ‚, чтŠ¾ Š±ŃƒŠ“ŠµŃ‚ хрŠ°Š½Šøть ŠŗŠ»ŃŽŃ‡Šø Š² тŠ¾Š¼ ŠæŠ¾Ń€ŃŠ“ŠŗŠµ, Š² ŠŗŠ¾Ń‚Š¾Ń€Š¾Š¼ Š²Ń‹ Šøх Š·Š°Š“Š°Š»Šø. Š•ŃŠ»Šø Š²Š°Š¶Š½Š¾ ŠæŠ¾Š»ŃƒŃ‡Šøть ŠøŠ· d.keys() Š¾Ń€ŠøŠ³ŠøŠ½Š°Š»ŃŒŠ½Ń‹Š¹ ŠæŠ¾Ń€ŃŠ“Š¾Šŗ, тŠ¾ Š“Š»Ń этŠ¾Š³Š¾ ŠµŃŃ‚ŃŒ OrderedDict.

InĀ [13]:
# ŠžŠæŠµŃ€Š°Ń†Šøя рŠµŠ²ŠµŃ€ŃŠ° Š½Š° сŠ»Š¾Š²Š°Ń€Šµ Š±ŠµŃŠ¼Ń‹ŃŠ»ŠµŠ½Š½Š° ā€” ŠæŠ¾Ń€ŃŠ“ŠŗŠ° Š½ŠµŃ‚ (Š“Š¾Š»Š¶Š½Š° Š±Ń‹Ń‚ŃŒ Š¾ŃˆŠøŠ±ŠŗŠ°)
dict_std = {"a": 1, "b": 2, "c": 3}
[k for k in reversed(dict_std)]
Out[13]:
['c', 'b', 'a']
InĀ [14]:
# Š Š°Š±Š¾Ń‡ŠøŠ¹ workaround Š² Python 3.7+
dict_std = {"a": 1, "b": 2, "c": 3}
[k for k in reversed(list(dict_std.keys()))]
Out[14]:
['c', 'b', 'a']
InĀ [15]:
# OrderedDict ŠæрŠµŠ“ŠæŠ¾Š»Š°Š³Š°ŠµŃ‚ Š½Š°Š»ŠøчŠøŠµ ŠæŠ¾Ń€ŃŠ“ŠŗŠ°, Š¼Š¾Š¶Š½Š¾ ŠæŠµŃ€ŠµŠ²ŠµŃ€Š½ŃƒŃ‚ŃŒ
from collections import OrderedDict 
dict_odr = OrderedDict({"a": 1, "b": 2, "c": 3})
[k for k in reversed(dict_odr)]
Out[15]:
['c', 'b', 'a']

defaultdict ā€” ŠŗŠ¾Š³Š“Š° Š»ŠµŠ½ŃŒ ŠøŠ½ŠøцŠøŠ°Š»ŠøŠ·ŠøрŠ¾Š²Š°Ń‚ŃŒ ŠŗŠ»ŃŽŃ‡ŠøĀ¶

Š•ŃŠ»Šø Š¾Š±Ń€Š°Ń‚Šøться Šŗ ŠŗŠ»ŃŽŃ‡Ńƒ сŠ»Š¾Š²Š°Ń€Ń, ŠŗŠ¾Ń‚Š¾Ń€Š¾Š³Š¾ Š½ŠµŃ‚, тŠ¾ Š±ŃƒŠ“ŠµŃ‚ Š¾ŃˆŠøŠ±ŠŗŠ°.

InĀ [16]:
# ŠžŠ±Ń‹Ń‡Š½Ń‹Š¹ сŠ»Š¾Š²Š°Ń€ŃŒ руŠ³Š°ŠµŃ‚ся Š½Š° ŠæŠ¾Š»Ń, ŠŗŠ¾Ń‚Š¾Ń€Ń‹Ń… Š½ŠµŃ‚ (Š“Š¾Š»Š¶Š½Š° Š±Ń‹Ń‚ŃŒ Š¾ŃˆŠøŠ±ŠŗŠ°)
d = {}
d["4"]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-16-c9aa461dd7f1> in <module>
      1 # ŠžŠ±Ń‹Ń‡Š½Ń‹Š¹ сŠ»Š¾Š²Š°Ń€ŃŒ руŠ³Š°ŠµŃ‚ся Š½Š° ŠæŠ¾Š»Ń, ŠŗŠ¾Ń‚Š¾Ń€Ń‹Ń… Š½ŠµŃ‚ (Š“Š¾Š»Š¶Š½Š° Š±Ń‹Ń‚ŃŒ Š¾ŃˆŠøŠ±ŠŗŠ°)
      2 d = {}
----> 3 d["4"]

KeyError: '4'

Š•ŃŠ»Šø Š½Š°Š“Š¾ ŠŗŠ°ŠŗŠ¾Šµ-тŠ¾ ŠæŠ¾Š²ŠµŠ“ŠµŠ½ŠøŠµ ŠæŠ¾-уŠ¼Š¾Š»Ń‡Š°Š½Šøю, тŠ¾ Š“Š»Ń этŠ¾Š³Š¾ уŠ“Š¾Š±Š½Š¾ ŠøсŠæŠ¾Š»ŃŒŠ·Š¾Š²Š°Ń‚ŃŒ defaultdict.

InĀ [17]:
# ŠŸŃ€ŠøŠ¼ŠµŃ€ с 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)
Out[17]:
[('Š°', 4),
 (' ', 3),
 ('Š·', 2),
 ('Š»', 2),
 ('Šø', 2),
 ('Š¾', 2),
 ('я', 1),
 ('Š²', 1),
 ('ю', 1),
 ('Š³', 1),
 ('Šŗ', 1),
 ('Šµ', 1),
 ('р', 1),
 ('с', 1),
 ('Š½', 1),
 ('Š¼', 1)]

ChainMap ā€”Ā ŠŗŠ¾Š³Š“Š° Š½ŃƒŠ¶Š½Š¾ Š½ŠµŃŠŗŠ¾Š»ŃŒŠŗŠ¾ сŠ»Š¾Š²Š°Ń€ŠµŠ¹ ŠæрŠµŠ“стŠ°Š²Šøть ŠŗŠ°Šŗ Š¾Š“ŠøŠ½Ā¶

ChainMap хрŠ°Š½Šøт ссыŠ»ŠŗŠø Š½Š° Š“руŠ³ŠøŠµ сŠ»Š¾Š²Š°Ń€Šø. ŠšŠ¾Š½Ń‚ŠµŠ¹Š½ŠµŃ€ ŠæŠ¾Š·Š²Š¾Š»ŃŠµŃ‚ рŠ°Š±Š¾Ń‚Š°Ń‚ŃŒ с Š²Š½ŃƒŃ‚Ń€ŠµŠ½Š½ŠøŠ¼Šø сŠ»Š¾Š²Š°Ń€ŃŠ¼Šø ŠŗŠ°Šŗ с Š¾Š“Š½ŠøŠ¼ цŠµŠ»Ń‹Š¼ сŠ»Š¾Š²Š°Ń€ŠµŠ¼.

Š£ ChainMap ŠµŃŃ‚ŃŒ Š“Š²Š° трŠ°Š“ŠøцŠøŠ¾Š½Š½Ń‹Ń… ŠæрŠøŠ»Š¾Š¶ŠµŠ½Šøя:

  • Š’Š¾Š·Š²Ń€Š°Ń‰Š°Ń‚ŃŒ Š·Š½Š°Ń‡ŠµŠ½Šøя ŠæŠ¾-уŠ¼ŠæŠ¾Š»Ń‡Š°Š½Šøю ŠøŠ· сŠ»Š¾Š²Š°Ń€Ń, ŠµŃŠ»Šø ŠŗŠ»ŃŽŃ‡Š° Š½ŠµŃ‚.
  • ŠŸŠ¾ŠøсŠŗ ŠæŠ¾ Š½ŠµŃŠŗŠ¾Š»ŃŒŠŗŠøх сŠ»Š¾Š²Š°Ń€ŃŠ¼.
InĀ [18]:
# ŠŸŃ€ŠøŠ¼ŠµŃ€ с ChainMap: ŠæŠ¾ŠøсŠŗ ŠæŠ¾ Š“Š²ŃƒŠ¼ сŠ»Š¾Š²Š°Ń€ŃŠ¼
from collections import ChainMap

group1 = {"a": 1, "b": 2}
group2 = {"c": 3, "d": 4}
groups = ChainMap(group1, group2)
groups["d"], groups["b"]
Out[18]:
(4, 2)

Š—Š“ŠµŃŃŒ group1 Š¼Š¾Š¶ŠµŃ‚ Š±Ń‹Ń‚ŃŒ сŠ»Š¾Š²Š°Ń€Ń‘Š¼ ŠæŠ°Ń€Š°Š¼ŠµŃ‚Ń€Š¾Š², Š° group2 ā€” сŠ»Š¾Š²Š°Ń€Ń‘Š¼ Š·Š½Š°Ń‡ŠµŠ½ŠøŠ¹ ŠæŠ°Ń€Š°Š¼ŠµŃ‚Ń€Š¾Š² ŠæŠ¾-уŠ¼Š¾Š»Ń‡Š°Š½Šøю. Š•ŃŠ»Šø Š·Š½Š°Ń‡ŠµŠ½Šøя Š½ŠµŃ‚ Š² group1, тŠ¾ Š²ŠµŃ€Š½Ń‘Ń‚ŃŃ Š·Š½Š°Ń‡ŠµŠ½ŠøŠµ ŠøŠ· group2.

MappingProxyType ā€” ŠŗŠ¾Š³Š“Š° Š½ŃƒŠ¶ŠµŠ½ сŠ»Š¾Š²Š°Ń€ŃŒ тŠ¾Š»ŃŒŠŗŠ¾ Š“Š»Ń чтŠµŠ½ŠøяĀ¶

InĀ [19]:
# ŠŸŃ€ŠøŠ¼ŠµŃ€ сŠ»Š¾Š²Š°Ń€Ń, Š² ŠŗŠ¾Ń‚Š¾Ń€Ń‹Š¹ Š½ŠµŠ»ŃŒŠ·Ń ŠæŠøсŠ°Ń‚ŃŒ
from types import MappingProxyType

d = {"a": 1, "b": 2} # ŠžŠ±Ń‹Ń‡Š½Ń‹Š¹ сŠ»Š¾Š²Š°Ń€ŃŒ
d_ro = MappingProxyType(d) # MappingProxyType (Read Only)

d_ro["a"]
Out[19]:
1
InĀ [20]:
# Š—Š°ŠæŠøсŠ°Ń‚ŃŒ Š² MappingProxyType Š½ŠµŠ»ŃŒŠ·Ń (Š“Š¾Š»Š¶Š½Š° Š±Ń‹Ń‚ŃŒ Š¾ŃˆŠøŠ±ŠŗŠ°)
d_ro["a"] = 1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-819d3d450b91> in <module>
      1 # Š—Š°ŠæŠøсŠ°Ń‚ŃŒ Š² MappingProxyType Š½ŠµŠ»ŃŒŠ·Ń (Š“Š¾Š»Š¶Š½Š° Š±Ń‹Ń‚ŃŒ Š¾ŃˆŠøŠ±ŠŗŠ°)
----> 2 d_ro["a"] = 1

TypeError: 'mappingproxy' object does not support item assignment

Counter ā€”Ā ŠŗŠ¾Š³Š“Š° Š½Š°Š“Š¾ Š¼ŃƒŠ»ŃŒŃ‚ŠøŠ¼Š½Š¾Š¶ŠµŃŃ‚Š²Š¾Ā¶

ŠœŃƒŠ»ŃŒŃ‚ŠøŠ¼Š½Š¾Š¶ŠµŃŃ‚Š²Š¾ ā€”Ā Š¼Š½Š¾Š¶ŠµŃŃ‚Š²Š¾, эŠ»ŠµŠ¼ŠµŠ½Ń‚Ń‹ Š² ŠŗŠ¾Ń‚Š¾Ń€Š¾Š¼ Š¼Š¾Š³ŃƒŃ‚ ŠæŠ¾Š²Ń‚Š¾Ń€ŃŃ‚ŃŒŃŃ.

InĀ [21]:
# ŠŸŃ€ŠøŠ¼ŠµŃ€ ŠæŠ¾ŃŃ‚Ń€Š¾ŠµŠ½Šøя Š¼ŃƒŠ»ŃŒŃ‚ŠøŠ¼Š½Š¾Š¶ŠµŃŃ‚Š²Š° ŠæŠ¾ ŠøтŠµŃ€ŠøруŠµŠ¼Š¾Š¼Ńƒ Š¾Š±ŃŠŠµŠŗту (стрŠ¾ŠŗŠµ)
from collections import Counter

s = 'я Š·Š°Š»ŠøŠ²Š°ŃŽ Š³Š»Š°Š·Š° ŠŗŠµŃ€Š¾ŃŠøŠ½Š¾Š¼'
c = Counter(s)
c
Out[21]:
Counter({'я': 1,
         ' ': 3,
         'Š·': 2,
         'Š°': 4,
         'Š»': 2,
         'Šø': 2,
         'Š²': 1,
         'ю': 1,
         'Š³': 1,
         'Šŗ': 1,
         'Šµ': 1,
         'р': 1,
         'Š¾': 2,
         'с': 1,
         'Š½': 1,
         'Š¼': 1})
InĀ [22]:
# sum ā€”Ā ŠŗŠ¾Š»ŠøчŠµŃŃ‚Š²Š¾ эŠ»ŠµŠ¼ŠµŠ½Ń‚Š¾Š² Š² Š¼Š½Š¾Š¶ŠµŃŃ‚Š²Šµ, 
# len - ŠŗŠ¾Š»ŠøчŠµŃŃ‚Š²Š¾ уŠ½ŠøŠŗŠ°Š»ŃŒŠ½Ń‹Ń… эŠ»ŠµŠ¼ŠµŠ½Ń‚Š¾Š² Š² Š¼Š½Š¾Š¶ŠµŃŃ‚Š²Šµ
sum(c.values()), len(c.values())
Out[22]:
(25, 16)

deque ā€” ŠŗŠ¾Š³Š“Š° Š½ŃƒŠ¶ŠµŠ½ Š“Š²ŃƒŃŃ‚Š¾Ń€Š¾Š½Š½ŠøŠ¹ Š“Š¾ŃŃ‚ŃƒŠæĀ¶

Š˜ Š¾Ń‡ŠµŃ€ŠµŠ“ь Šø стŠµŠŗ Š¼Š¾Š¶Š½Š¾ рŠµŠ°Š»ŠøŠ·Š¾Š²Š°Ń‚ŃŒ Š½Š° list. ŠŠ¾ list рŠµŠ°Š»ŠøŠ·Š¾Š²Š°Š½ Š½Š° Š“ŠøŠ½Š°Š¼ŠøчŠµŃŠŗŠ¾Š¼ Š¼Š°ŃŃŠøŠ²Šµ, Š° deque Š½Š° сŠ²ŃŠ·Š½Š¾Š¼ сŠæŠøсŠŗŠµ.

InĀ [23]:
# ŠŸŃ€ŠøŠ¼ŠµŃ€ Š“Š²ŃƒŃŃ‚Š¾Ń€Š¾Š½Š½ŠµŠ¹ Š¾Ń‡ŠµŃ€ŠµŠ“Šø
from collections import deque
d = deque()
d.append("я")
d.append("ŠøŠ“у")
d.append("Š±Ń€Š°Ń‚ŃŒ")
d.append("Š»ŃƒŃ‚")
d.popleft(), d.pop()
Out[23]:
('я', 'Š»ŃƒŃ‚')

heapq ā€”Ā ŠŗŠ¾Š³Š“Š° Š½ŃƒŠ¶Š½Š° Š¾Ń‡ŠµŃ€ŠµŠ“ь с ŠæрŠøŠ¾Ń€ŠøтŠµŃ‚Š¾Š¼Ā¶

InĀ [24]:
# ŠŸŃ€ŠøŠ¼ŠµŃ€
from heapq import heappush, heappop

q = []
heappush(q, (3, "ŠŸŃ€Š¾Š“Š°ŠŗшŠµŠ½"))
heappush(q, (1, "Š§ŠæŠ¾Šŗ"))
heappush(q, (2, "Š§ŠæŠ¾Šŗ"))

heappop(q),heappop(q),heappop(q)
Out[24]:
((1, 'Š§ŠæŠ¾Šŗ'), (2, 'Š§ŠæŠ¾Šŗ'), (3, 'ŠŸŃ€Š¾Š“Š°ŠŗшŠµŠ½'))

dataclasses ā€”Ā ŠŗŠ¾Š³Š“Š° Š¾Ń‚ ŠŗŠ»Š°ŃŃŠ° Š½Š°Š“Š¾ тŠ¾Š»ŃŒŠŗŠ¾ хрŠ°Š½ŠµŠ½ŠøŠµ Š“Š°Š½Š½Ń‹Ń…Ā¶

ŠšŠ¾Š³Š“Š° Š½ŃƒŠ¶ŠµŠ½ ŠøŠ·Š¼ŠµŠ½ŃŠµŠ¼Ń‹Š¹ namedtuple, тŠ¾ ŠæŠ¾Š“хŠ¾Š“Šøт dataclasses.

InĀ [25]:
# ŠŸŃ€ŠøŠ¼ŠµŃ€ с сŠ¾Š±ŃŃ‚Š²ŠµŠ½Š½Ń‹Š¼ 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
Out[25]:
4

ŠŸŠ¾-уŠ¼Š¾Š»Ń‡Š°Š½Šøю dataclass Š³ŠµŠ½ŠµŃ€ŠøруŠµŃ‚ 3 Š“Š°Š½Š“ŠµŃ€-Š¼ŠµŃ‚Š¾Š“Š° (Š¼ŠµŃ‚Š¾Š“ с Š“Š²Š¾Š¹Š½Ń‹Š¼ ŠæŠ¾Š“чŠµŃ€ŠŗŠøŠ²Š°Š½ŠøŠµŠ¼):

  • init,
  • repr,
  • eq.

Š§ŠµŃ€ŠµŠ· Š¾ŠæцŠøŠø Š“ŠµŠŗŠ¾Ń€Š°Ń‚Š¾Ń€Š° Š¼Š¾Š¶Š½Š¾ сŠ“ŠµŠ»Š°Ń‚ŃŒ dataclass immutable (frozen), mutable-хэшŠøруŠµŠ¼Ń‹Š¼ (unsafe_hash) Šø срŠ°Š²Š½ŠøŠ²Š°ŠµŠ¼Ń‹Š¼ (order).

title

InĀ [26]:
%load_ext watermark
%watermark -d -u -v -iv
last updated: 2019-10-21 

CPython 3.8.0
IPython 7.8.0