@Pythonetcコンパイル2019年3月


これは、私の@pythonetcフィードからのPythonのヒントとプログラミングの10番目のコレクションです。

前の選択

0_0


0_0はPythonの完全に正しい表現です。

なしでリストを並べ替える


None値でリストをソートするのは大変な作業です。

 In [1]: data = [  ...: dict(a=1),  ...: None,  ...: dict(a=-3),  ...: dict(a=2),  ...: None,  ...: ] In [2]: sorted(data, key=lambda x: x['a']) ... TypeError: 'NoneType' object is not subscriptable 

すべてのNoneを削除して、ソート後に(タスクに応じてリストの先頭または末尾に)戻してみることができます。

 In [3]: sorted(  ...: (d for d in data if d is not None),  ...: key=lambda x: x['a']  ...: ) + [  ...: d for d in data if d is None  ...: ] Out[3]: [{'a': -3}, {'a': 1}, {'a': 2}, None, None] 

しかし、それは不便です。 より複雑なkey使用する方が良い:

 In [4]: sorted(data, key=lambda x: float('inf') if x is None else x['a']) Out[4]: [{'a': -3}, {'a': 1}, {'a': 2}, None, None] 

無限が受け入れられない型について話している場合、タプルをソートできます。

 In [5]: sorted(data, key=lambda x: (1, None) if x is None else (0, x['a'])) Out[5]: [{'a': -3}, {'a': 1}, {'a': 2}, None, None] 

random.seed()を呼び出す


プロセスを分岐すると、使用するランダムシードが、結果のすべてのプロセスにコピーされます。 その結果、同じ「ランダムな」結果が生成される可能性があります。

これを回避するには、各プロセスでrandom.seed()を手動で呼び出す必要があります。 ただし、 multiprocessingモジュールを使用する場合は、自動的に実行されます。

例:

 import multiprocessing        import random                 import os                     import sys                    def test(a):                  print(random.choice(a), end=' ') a = [1, 2, 3, 4, 5]           for _ in range(5):            test(a)                   print()                       for _ in range(5):            p = multiprocessing.Process(   target=test, args=(a,) )                         p.start()                 p.join()                  print()                       for _ in range(5):            pid = os.fork()           if pid == 0:              test(a)               sys.exit()            else:                     os.wait()             print() 

次のようなものを入手してください。

 4 4 4 5 5 1 4 1 3 3 2 2 2 2 2 

さらに、Python 3.7以降を使用している場合、新しいat_fork at_forkおかげで、os.fork でも同じことができます。

上記のPython 3.7コードは次の結果を提供します。

 1 2 2 1 5 4 4 4 5 5 2 4 1 3 1 

0への加算


一見すると、 sum([a, b, c]) a + b + c sum([a, b, c])等価であるように見えますが、実際には等価は0 + a + b + cです。 そのため、この式は0への加算をサポートしないタイプでは機能しません。

 class MyInt: def __init__(self, value): self.value = value def __add__(self, other): return type(self)(self.value + other.value) def __radd__(self, other): return self + other def __repr__(self): class_name = type(self).__name__ return f'{class_name}({self.value})' In : sum([MyInt(1), MyInt(2)]) ... AttributeError: 'int' object has no attribute 'value' 

これを修正するために、 0代わりに使用されるカスタム開始要素を提供できます。

 In : sum([MyInt(1), MyInt(2)], MyInt(0)) Out: MyInt(3) 

sum float型とint型を追加するように設計されていますが、他のカスタム型でも機能します。 ただし、彼はbytesbytearray 、およびstrを追加することを拒否していbytes 。これjoinjoinこの目的のためであるためです。

 In : sum(['a', 'b'], '') ... TypeError: sum() can't sum strings [use ''.join(seq) instead] In : ints = [x for x in range(10_000)] In : my_ints = [Int(x) for x in ints] In : %timeit sum(ints) 68.3 µs ± 142 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each) In : %timeit sum(my_ints, Int(0)) 5.81 ms ± 20.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) 



Jupyter Notebookのインデックスの完了


_ipython_key_completions_メソッドを使用して、Jupyterノートブックのインデックス補完をカスタマイズできます。 このように、 d["xようなものの後にTabを押すと、画面に表示されるものを制御できます。



メソッドは、検索する文字列を引数として受け取らないことに注意してください。

Source: https://habr.com/ru/post/J447210/


All Articles