LoginSignup
1
2

More than 5 years have passed since last update.

[作成中]Python使い方メモ

Last updated at Posted at 2018-10-16

Pythonでコードを書く際の注意点、気になった点を記載していきます。
また、Kaggleで学んだ書き方も備忘録として加えて行きます。

変数のアンダーバーについて

Pythonの標準コーディングスタイルを定めるPEP 8にてガイドラインが説明されています。以下、日本語訳バージョンから部分引用します:
https://pep8-ja.readthedocs.io/ja/latest/#id24

アンダーバー1つ

_single_leading_underscore: “内部でだけ使う” ことを示します。
たとえばfrom M import *は、アンダースコアで始まる名前のオブジェクトをインポートしません。

アンダーバー2つ

__double_leading_underscore: クラスの属性に名前を付けるときに、名前のマングリング機構を呼び出します (クラスFoobarの__booという名前は_FooBar__booになります。以下も参照してください)

__double_leading_and_trailing_underscore__: ユーザーが制御する名前空間に存在する "マジック"オブジェクト または "マジック"属性です。 たとえば __init__, __import__, __file__ が挙げられます。この手の名前を再発明するのはやめましょう。ドキュメントに書かれているものだけを使ってください。

math関数

平方根を求める

Python3
>>>import math
>>>math.sqrt(7)
2.6457513110645907

マルチコア並列処理

膨大なデータを分析する必要がある場合、処理を高速化をできる。

Python
from multiprocessing import Pool
from multiprocessing import Process

def function(hoge):
    #やりたいこと
    return x

def multi(n):
    p = Pool(10) #最大プロセス数:10
    result = p.map(function, range(n))
    return result

def main():
    data = multi(20)
    for i in data:
        print i

main()

「0,1,2・・・19と値を変化させてfunctionを20回実行する」という処理です。
functionの返値はresultにリストで入っているので、受け取って標準出力しています。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2