よく考えれば当たり前だったはずなんですが、ハマってしまいました。 何かと言えば、デフォルト引数の評価です。 自戒のメモ。 デフォルト引数値の評価は一回 デフォルト引数は便利なんですが、一回しか評価されないので、 dictやlist等のミュータブル(変更可能)な値を使う場合は、 変更されることに注意しないといけない。 というのは一応常識のはずで、チュートリアルにもきちんと書いてあり、 自分もちゃんと読んだ記憶がある。 例えばこのfoo()の例。 チュートリアルにある例と本質は同じですが、 __init()__でも同じことがおこります。 # -*- coding:utf-8 -*- class foo(): L='' def __init__(self,l = []): l.append('a') self.L=l if __name__ == '__main__': f1=foo() pri
「パーフェクトpython」読んでます。 パーフェクトPython (PERFECT SERIES 5) 作者:Pythonサポーターズ,露木 誠,ルイス・イアン,石本 敦夫,小田 切篤,保坂 翔馬,大谷 弘喜技術評論社Amazon 1個1個のトピックについて非常に説明が詳しいので、python という言語を勉強するのに役立ちます。 今回は「5章 関数」を読んでいて「これは知らなかった!」ということがあったのでメモ。 pythonで関数を定義する時、引数にデフォルト引数を設定することができますよね。 def hoge(foo, bar, baz='xxx'): print '%s:%s:%s' % (foo, bar, baz) この関数を呼び出す時に引数bazを省略すると、デフォルト引数として設定した'xxx'が使われます。 >>> hoge('aaa', 'bbb', 'ccc') a
R を使っている方はご存知だと思うが、R には {htmlwidgets} というパッケージがあり、R 上のデータを任意の Javascript ライブラリを使ってプロットすることが比較的カンタンにできる。{htmlwidgets} って何?という方には こちらの説明がわかりやすい。 RPubs - htmlwidgetsでJavascriptの可視化をRに 同じことを Python + pandas を使ってやりたい。サンプルとして利用する Javascript ライブラリは 上の資料と同じく Highcharts、Highstock にする。 www.highcharts.com 補足 pandas-highcharts という Python パッケージもあるが、このエントリでは任意の Javascript ライブラリで使えるであろう方法を記載する。 Highcharts でのプロット
[Python] クラスに入門(宣言、インスタンス変数/メソッド、クラス変数/メソッド、スタティックメソッド、継承) クラスの基礎 Pythonは他の多くの言語と同じく、クラスを定義して使うことができます。具体的には以下のように定義します。 # クラスの定義 class MyClass(object): pass # クラスからインスタンスを生成 me = MyClass() print(me) # => <__main__.MyClass object at 0x100799b00> これで何も要素を持たないクラスを定義できました。ちなみにこの「何も要素を持たない」クラスは以外と便利なのです。 # 何も要素を持たないクラス class EmptyClass(object): pass # 要素を好きなように定義できるので、複数の変数をまとめた入れ物として扱える holder = Empt
はじめに Pythonのlist型(リスト)へのデータへの追加、結合の仕方のまとめです。他プログラミング言語の配列と同様に扱えます。 目次 はじめに 先頭の削除 - pop 末尾の削除 - pop 任意の位置の削除 - pop, del 複数の要素の削除 - del 指定した値と一致した場合に削除 - remove 全ての要素の削除 - clear, del 先頭の削除 - pop リストの先頭の要素を削除します。popの引数に0を指定します。しかし、popをリストの先頭に対しておこなうとパフォーマンスは悪いです。処理としては、他の要素を一つずつずらしているためです。 list = [1, 2, 3] list.pop(0) print(list) # [2, 3] 末尾の削除 - pop リストの末尾の要素を削除します。popの引数に何も指定しない場合、末尾が削除されます。 list =
What’s New In Python 3.6¶ Editors: Elvis Pranskevichus <elvis@magic.io>, Yury Selivanov <yury@magic.io> This article explains the new features in Python 3.6, compared to 3.5. Python 3.6 was released on December 23, 2016. See the changelog for a full list of changes. Summary – Release highlights¶ New syntax features: PEP 498, formatted string literals. PEP 515, underscores in numeric literals. PEP
Simple Traces Distribution Traces Finance Traces 3D Traces Map Traces Specialized Traces Layout Axes and Subplots Layers Python Figure Reference: Single-Page This page is the exhaustive reference for all of the attributes in the core figure data structure that the plotly library operates on. It is automatically-generated from the machine-readable Plotly.js schema reference. Figures are represented
Time Series and Date Axes in Python How to plot date and time in python. New to Plotly? Plotly is a free and open-source graphing library for Python. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials. Time Series using Axes of type date¶Time series
Environments# An environment is a directory that contains a specific collection of packages that you have installed. For example, you may have one environment with NumPy 1.7 and its dependencies, and another environment with NumPy 1.6 for legacy testing. If you change one environment, your other environments are not affected. You can easily activate or deactivate environments, which is how you swi
pythonで株やFXなんかで使うローソク足チャートを書きたかったのですが、ちょっと苦戦したのでメモ。 完成品はこんな感じです。 ソースコードはこんな感じ。 Copy import pandas import matplotlib.pyplot as plt from matplotlib.finance import candlestick_ohlc dat = pandas.read_csv('usdjpy.csv', parse_dates=['日付']) # ファイルの読み込み。 dat = dat[-50:] # データが多すぎるので減らす。 dates = dat['日付'] # あとでつかう。 tmp = dat['日付'].values.astype('datetime64[D]') # ナノ秒精度とか無意味なので、精度を日単位まで落とす。 dat['日付'] = tmp.
準備 データ処理用にnumpy、プロット用にpyplot、3次元なのでmpl_toolkits.mplot3dをインポートします。 from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np 描画するデータの作成 3次元で描画するにはメッシュ(2次元の網目)を作成するために2次元の配列を用意する必要があります。 まずarangeメソッドでx,yそれぞれを1次元領域で分割します。 x = np.arange(-3, 3, 0.25) y = np.arange(-3, 3, 0.25) 2次元メッシュを作成するにはmeshgridメソッドを利用します。この関数の戻り値はX,Yに対応する行列で、Xは行にxの配列を、Yは列にyの配列を入れたものになっています。 X, Y =
As I know, %debug magic can do debug within one cell. However, I have function calls across multiple cells. For example, In[1]: def fun1(a) def fun2(b) # I want to set a breakpoint for the following line # return do_some_thing_about(b) return fun2(a) In[2]: import multiprocessing as mp pool=mp.Pool(processes=2) results=pool.map(fun1, 1.0) pool.close() pool.join What I tried: I tried to set %debug
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く