{"id":204476,"date":"2013-12-02T21:05:03","date_gmt":"2013-12-02T17:05:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=204476"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=204476","title":{"rendered":"<span class=\"post_title\">\u041f\u0438\u0448\u0435\u043c \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0439, \u0438\u0434\u0438\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 Python<\/span>"},"content":{"rendered":"<div class=\"content html_format\"> \t\t\t<img decoding=\"async\" src=\"http:\/\/habr.habrastorage.org\/post_images\/f54\/f36\/d44\/f54f36d44609afab8cf09e26f78d666b.jpg\" align=\"left\"\/><br \/>  \u0418\u043d\u043e\u0433\u0434\u0430 \u0442\u044f\u0436\u0435\u043b\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 \u0421\u0435\u0442\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435, \u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u00abbest practices\u00bb \u0434\u043b\u044f \u044f\u0437\u044b\u043a\u0430. \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0441\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u043d\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u043d\u0430\u0439\u0442\u0438 \u043d\u0443\u0436\u043d\u0443\u044e \u0432\u0435\u0449\u044c \u0432 \u0430\u0431\u0437\u0430\u0446\u0430\u0445 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0433\u043e (\u043d\u0430 \u0442\u043e \u043e\u043d\u0430 \u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f) \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u043e. \u041d\u043e \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043c\u043d\u0435 \u0443\u043b\u044b\u0431\u043d\u0443\u043b\u0441\u044f \u043f\u043e\u0438\u0441\u043a Google, \u0438 \u044f \u043d\u0430\u0442\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u00ab\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b\u00bb \u044f\u0437\u044b\u043a\u0430 Python \u043e\u0442 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 core \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u044f\u0437\u044b\u043a\u0430 \u2014 Raymond Hettinger.<\/p>\n<p>  <i>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/i>: \u0412\u0441\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u044b \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u0445: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0434\u0443\u0442 \u0441\u0430\u043c\u044b\u0435 \u00ab\u043f\u043b\u043e\u0445\u0438\u0435\u00bb \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u0430 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430. \u0410\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0432\u0435\u0440\u0441\u0438\u0438 \u044f\u0437\u044b\u043a\u0430 2.7, \u043e\u0442\u043b\u0438\u0447\u0438\u044f \u0434\u043b\u044f \u0432\u0435\u0440\u0441\u0438\u0438 3.3 \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0432 \u043f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u044f\u0445 \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c\u0443 \u00ab\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0443\u00bb.<br \/>   <a name=\"habracut\"><\/a>  <\/p>\n<h5>\u0426\u0438\u043a\u043b \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0438\u0437 \u0447\u0438\u0441\u0435\u043b<\/h5>\n<p>  <b>\u041f\u043b\u043e\u0445\u043e<\/b>: \u0438\u043d\u043e\u0433\u0434\u0430 \u043f\u0438\u0448\u0443\u0442 \u0442\u0430\u043a.   <\/p>\n<pre><code class=\"python\">for i in [0, 1, 2, 3, 4, 5]:     print i**2 <\/code><\/pre>\n<p>  <b>\u0425\u043e\u0440\u043e\u0448\u043e<\/b>: \u043b\u0443\u0447\u0448\u0438\u0439, \u0441 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c. \u041d\u043e \u0432 32 \u0431\u0438\u0442\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0441\u043f\u0438\u0441\u043e\u043a \u0438\u0437 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0447\u0438\u0441\u0435\u043b \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c ~ 32 mb \u043f\u0430\u043c\u044f\u0442\u0438.   <\/p>\n<pre><code class=\"python\">for i in range(6):     print i**2 <\/code><\/pre>\n<p>  <b>\u041e\u0442\u043b\u0438\u0447\u043d\u043e:<\/b> \u0441\u0430\u043c\u043e \u043b\u0443\u0447\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442. \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0438 \u043e\u0442 \u0432\u0442\u043e\u0440\u043e\u0433\u043e <code>xrange<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0437\u0430 \u0440\u0430\u0437, \u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0448\u043d\u044e\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430.   <\/p>\n<pre><code class=\"python\">for i in xrange(6):     print i**2 <\/code><\/pre>\n<p>  <i>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/i>: \u0412 \u0432\u0435\u0440\u0441\u0438\u0438 Python 3.3 <code>xrange<\/code> \u0443\u0436\u0435 \u0432 \u044f\u0434\u0440\u0435 \u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u044c\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e <code>range<\/code>. <\/p>\n<h5>\u0426\u0438\u043a\u043b \u043f\u043e \u0441\u043f\u0438\u0441\u043a\u0443<\/h5>\n<p>  <b>\u041f\u043b\u043e\u0445\u043e<\/b>: \u0447\u0430\u0441\u0442\u043e \u0431\u044b\u0432\u0448\u0438\u0435 \u0421 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b \u043f\u0438\u0448\u0443\u0442 \u0442\u0430\u043a.  <\/p>\n<pre><code class=\"python\">colors = ['red', 'green', 'blue', 'yellow'] for i in range(len(colors)):     print colors[i] <\/code><\/pre>\n<p>  <b>\u0425\u043e\u0440\u043e\u0448\u043e<\/b>: \u043b\u0443\u0447\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442.   <\/p>\n<pre><code class=\"python\">colors = ['red', 'green', 'blue', 'yellow'] for color in colors:     print color <\/code><\/pre>\n<p>  \u041d\u043e \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e \u0441\u043f\u0438\u0441\u043a\u0443 \u0437\u0430\u0434\u043e\u043c \u043d\u0430 \u043f\u0435\u0440\u0435\u0434? <\/p>\n<p>  <b>\u041f\u043b\u043e\u0445\u043e<\/b>: \u043e\u043f\u044f\u0442\u044c, \u043f\u0440\u043e\u0448\u043b\u043e \u0438\u0437 C \u0434\u0430\u0435\u0442 \u043e \u0441\u0435\u0431\u0435 \u0437\u043d\u0430\u0442\u044c:  <\/p>\n<pre><code class=\"python\">colors = ['red', 'green', 'blue', 'yellow'] for i in range(len(colors)):     print colors[i] <\/code><\/pre>\n<p>  <b>\u0425\u043e\u0440\u043e\u0448\u043e<\/b>: \u043d\u043e \u0432 Python \u043f\u0438\u0448\u0443\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a:   <\/p>\n<pre><code class=\"python\">colors = ['red', 'green', 'blue', 'yellow'] for color in reversed(colors):     print color <\/code><\/pre>\n<h5>\u0426\u0438\u043a\u043b \u043f\u043e \u0441\u043f\u0438\u0441\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\u0438<\/h5>\n<p>  <b>\u041f\u043b\u043e\u0445\u043e<\/b> \u0442\u043e\u0436\u0435 \u0447\u0442\u043e \u0438 \u0432\u044b\u0448\u0435.   <\/p>\n<pre><code class=\"python\">colors = ['red', 'green', 'blue', 'yellow'] for i in range(len(colors)):     print i, '--&gt;', colors[i] <\/code><\/pre>\n<p>  <b>\u0425\u043e\u0440\u043e\u0448\u043e<\/b>: \u0431\u043e\u043b\u0435\u0435 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442:   <\/p>\n<pre><code class=\"python\">colors = ['red', 'green', 'blue', 'yellow'] for i, color in enumerate(colors):     print i, '--&gt;', color <\/code><\/pre>\n<h5>\u0426\u0438\u043a\u043b \u043f\u043e \u0434\u0432\u0443\u043c \u0441\u043f\u0438\u0441\u043a\u0430\u043c<\/h5>\n<p>  <b>\u041f\u043b\u043e\u0445\u043e<\/b> \u0442\u043e\u0436\u0435 \u0447\u0442\u043e \u0438 \u0432\u044b\u0448\u0435.   <\/p>\n<pre><code class=\"python\">names = ['raymond', 'rachel', 'matthew'] colors = ['red', 'green', 'blue', 'yellow'] n = min(len(names), len(colors)) for i in range(n):     print names[i], '--&gt;', colors[i] <\/code><\/pre>\n<p>  <b>\u0425\u043e\u0440\u043e\u0448\u043e<\/b>: \u0434\u0435\u043b\u0430\u0435\u043c \u0441 \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u043e\u0434\u0438\u043d \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c \u0447\u0442\u043e <code>zip <\/code>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0447\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442.  <\/p>\n<pre><code class=\"python\">names = ['raymond', 'rachel', 'matthew'] colors = ['red', 'green', 'blue', 'yellow'] for name, color in zip(names, colors):     print name, '--&gt;', color <\/code><\/pre>\n<p>  <b>\u041e\u0442\u043b\u0438\u0447\u043d\u043e<\/b>: \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0438 \u043e\u0442 <code>zip<\/code>, <code>izip <\/code>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c.   <\/p>\n<pre><code class=\"python\">names = ['raymond', 'rachel', 'matthew'] colors = ['red', 'green', 'blue', 'yellow'] for name, color in izip(names, colors):     print name, '--&gt;', color <\/code><\/pre>\n<p>  <i>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/i>: \u0412 \u0432\u0435\u0440\u0441\u0438\u0438 Python 3.3 <code>izip <\/code>\u0432\u043f\u0438\u0441\u0430\u043d \u0432 \u044f\u0434\u0440\u043e \u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u044c\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e <code>zip<\/code>. <\/p>\n<h5>\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443<\/h5>\n<p>  <b>\u041f\u043b\u043e\u0445\u043e<\/b>: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f.  <\/p>\n<pre><code class=\"python\">colors = ['red', 'green', 'blue', 'yellow']  def compare_length(c1, c2):     if len(c1) &lt; len(c2): return -1     if len(c1) &gt; len(c2): return 1     return 0  print sorted(colors, cmp=compare_length) <\/code><\/pre>\n<p>  <b>\u0425\u043e\u0440\u043e\u0448\u043e<\/b>: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e \u043a\u043b\u044e\u0447\u0443. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438.  <\/p>\n<pre><code class=\"python\">colors = ['red', 'green', 'blue', 'yellow']  print sorted(colors, key=len) <\/code><\/pre>\n<p>  <i>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/i>: \u041c\u0435\u0442\u043e\u0434 <code>cmp <\/code>\u0443\u0431\u0440\u0430\u043d \u0441 \u044f\u0434\u0440\u0430 Python 3.x. <\/p>\n<h5>\u0426\u0438\u043a\u043b \u043f\u043e \u043a\u043b\u044e\u0447\u0430\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u044f<\/h5>\n<p>  \u041e\u0431\u044b\u0447\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043b\u044e\u0447\u0438. \u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u0446\u0438\u043a\u043b\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u0432\u0430\u0440\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0435\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f.   <\/p>\n<pre><code class=\"python\">for k in d:     print k <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0446\u0438\u043a\u043b \u043f\u043e \u043a\u043b\u044e\u0447\u0430\u043c (\u041f\u0440\u0438\u043c\u0435\u0440: \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u043a\u043b\u044e\u0447\u0435\u0439 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u0441 <code>R<\/code>):  <\/p>\n<pre><code class=\"python\">for k in d.keys():     if k.startswith('R'):         del d[k] <\/code><\/pre>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <code>d.keys()<\/code> \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u043f\u0438\u044e \u043a\u043b\u044e\u0447\u0435\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044f, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439. <\/p>\n<h5>\u0426\u0438\u043a\u043b \u043f\u043e \u043a\u043b\u044e\u0447\u0430\u043c \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u044f<\/h5>\n<p>  <b>\u041f\u043b\u043e\u0445\u043e<\/b>: \u0446\u0438\u043a\u043b \u043f\u043e \u043a\u043b\u044e\u0447\u0430\u043c \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c. \u041c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431:  <\/p>\n<pre><code class=\"python\">for k in d:     print k, '--&gt;', d[k] <\/code><\/pre>\n<p>  <b>\u0425\u043e\u0440\u043e\u0448\u043e<\/b>: \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u0446\u0438\u043a\u043b \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u0445:  <\/p>\n<pre><code class=\"python\">for k, v in d.items():     print k, '--&gt;', v <\/code><\/pre>\n<p>  <b>\u041e\u0442\u043b\u0438\u0447\u043d\u043e<\/b>: \u041d\u043e \u0441\u0430\u043c\u043e \u043b\u0443\u0447\u0448\u0438\u0439 \u0438 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440:  <\/p>\n<pre><code class=\"python\">for k, v in d.iteritems():     print k, '--&gt;', v <\/code><\/pre>\n<h5>\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0432 \u043e\u0434\u0438\u043d \u0441\u043b\u043e\u0432\u0430\u0440\u044c<\/h5>\n<p>  \u041e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043c\u0435\u0442\u043e\u0434, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043a\u043e\u0440\u0442\u0435\u0436 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043b\u043e\u0432\u0430\u0440\u044f.   <\/p>\n<pre><code class=\"python\">names = ['raymond', 'rachel', 'matthew'] colors = ['red', 'green', 'blue']  d = dict(izip(names, colors)) # d \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:  # {'matthew': 'blue', 'rachel': 'green', 'raymond': 'red'} <\/code><\/pre>\n<h5>\u041f\u043e\u0434\u0441\u0447\u0435\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435<\/h5>\n<p>  <b>\u041f\u043b\u043e\u0445\u043e<\/b>: \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431:  <\/p>\n<pre><code class=\"python\">colors = ['red', 'green', 'red', 'blue', 'green', 'red'] d = {}  for color in colors:     if color not in d:         d[color] = 0     d[color] += 1  #{'blue': 1, 'green': 2, 'red': 3} <\/code><\/pre>\n<p>  <b>\u0425\u043e\u0440\u043e\u0448\u043e<\/b>: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>get()<\/code>:  <\/p>\n<pre><code class=\"python\">colors = ['red', 'green', 'red', 'blue', 'green', 'red'] d = {}  for color in colors:     d[color] = d.get(color, 0) + 1 <\/code><\/pre>\n<p>  <b>\u041e\u0442\u043b\u0438\u0447\u043d\u043e<\/b>: \u0441\u0430\u043c\u043e \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>defaultdict()<\/code>. \u041d\u043e \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0437\u043d\u0430\u0442\u044c <a href=\"http:\/\/docs.python.org\/dev\/library\/collections#collections.defaultdict\">\u043a\u0430\u043a \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/a>.   <\/p>\n<pre><code class=\"python\">d = defaultdict(int) for color in colors:     d[color] += 1 <\/code><\/pre>\n<h5>\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430<\/h5>\n<p>  <b>\u041f\u043b\u043e\u0445\u043e<\/b>: \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0441\u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0443 (\u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u2014 \u0434\u043b\u0438\u043d\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438) \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434:   <\/p>\n<pre><code class=\"python\">names = ['raymond', 'rachel', 'matthew', 'roger', 'betty', 'melissa', 'judith', 'charlie'] d = {}  for name in names:     key = len(name)     if key not in d:         d[key] = []     d[key].append(name) {5: ['roger', 'betty'], 6: ['rachel', 'judith'], 7: ['raymond', 'matthew', 'melissa', 'charlie']} <\/code><\/pre>\n<p>  <b>\u0425\u043e\u0440\u043e\u0448\u043e<\/b>: \u043d\u043e \u0435\u0441\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u0435\u0439 \u0438 \u0431\u044b\u0441\u0442\u0440\u0435\u0435:  <\/p>\n<pre><code class=\"python\">d = defaultdict(list) for name in names:     key = len(name)     d[key].append(name) <\/code><\/pre>\n<h5>\u0418\u0442\u043e\u0433<\/h5>\n<p>  \u041d\u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u0441\u0435. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u044d\u0442\u0438 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u044b\u0435, \u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043a\u043e\u043c\u0443-\u0442\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u043a\u043e\u0434, \u043a\u0430\u043a \u043e\u043d\u0438 \u043f\u043e\u043c\u043e\u0433\u043b\u0438 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043c\u043d\u0435. \u0418\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f Raymond Hettinger (<a href=\"https:\/\/twitter.com\/raymondh\">@raymondh<\/a>), Python Core Developer. \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/204476\/\"> http:\/\/habrahabr.ru\/post\/204476\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\"> \t\t\t<img decoding=\"async\" src=\"http:\/\/habr.habrastorage.org\/post_images\/f54\/f36\/d44\/f54f36d44609afab8cf09e26f78d666b.jpg\" align=\"left\"\/><br \/>  \u0418\u043d\u043e\u0433\u0434\u0430 \u0442\u044f\u0436\u0435\u043b\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 \u0421\u0435\u0442\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435, \u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u00abbest practices\u00bb \u0434\u043b\u044f \u044f\u0437\u044b\u043a\u0430. \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0441\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u043d\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u043d\u0430\u0439\u0442\u0438 \u043d\u0443\u0436\u043d\u0443\u044e \u0432\u0435\u0449\u044c \u0432 \u0430\u0431\u0437\u0430\u0446\u0430\u0445 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0433\u043e (\u043d\u0430 \u0442\u043e \u043e\u043d\u0430 \u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f) \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u043e. \u041d\u043e \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043c\u043d\u0435 \u0443\u043b\u044b\u0431\u043d\u0443\u043b\u0441\u044f \u043f\u043e\u0438\u0441\u043a Google, \u0438 \u044f \u043d\u0430\u0442\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u00ab\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b\u00bb \u044f\u0437\u044b\u043a\u0430 Python \u043e\u0442 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 core \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u044f\u0437\u044b\u043a\u0430 \u2014 Raymond Hettinger.<\/p>\n<p>  <i>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/i>: \u0412\u0441\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u044b \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u0445: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0434\u0443\u0442 \u0441\u0430\u043c\u044b\u0435 \u00ab\u043f\u043b\u043e\u0445\u0438\u0435\u00bb \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u0430 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430. \u0410\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0432\u0435\u0440\u0441\u0438\u0438 \u044f\u0437\u044b\u043a\u0430 2.7, \u043e\u0442\u043b\u0438\u0447\u0438\u044f \u0434\u043b\u044f \u0432\u0435\u0440\u0441\u0438\u0438 3.3 \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0432 \u043f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u044f\u0445 \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c\u0443 \u00ab\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0443\u00bb.   <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-204476","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/204476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=204476"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/204476\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=204476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=204476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}