スキップしてメイン コンテンツに移動

Handle space in Python


日本語ver

Introduction

I want to display following print in Command line.
apple
  apple
    apple
      apple
        apple

Miss

However, This print is not able to display following code used print script.
def main():
    i = 0
    while i<5:
        print('apple')
        j = 0
        while j < i:
            print(' ')
            j = j + 1
        i = i + 1
if __name__ == '__main__':
    main()
rusult…
enter image description here
Print code change line when writing new apple.

succeed

Please, look at following code.
import sys

def main():
    i = 0
    while i < 5:
        print('apple')
        j = 0
        while j <= i:
            sys.stdout.write(' ')
            j = j + 1
        i  = i + 1
if __name__ == '__main__':
    main()
sys.stdout.write is write string regardless of the changing line.
Result!
enter image description here
However, I give you a strong warning that sys.stdout.weite use only str.

Reference

https://www.lifewithpython.com/2013/12/python-print-without-.html

コメント

このブログの人気の投稿

離散フーリエ変換

Introduction English ver 今日は離散フーリエ変換について書きます。 現在、シグナル解析についてのpdfを作成中です。このpdfは github で公開中です。 シグナル解析は、courseraのレクチャーで勉強中です。 ここ にリンクを貼っておきます。 pdfは随時更新中です。

Theorem of K-means

Introduction 日本語 ver Today, I will write about the theorem of K-means algorithm. K-means algorithm is a method to do clustering about K of class. The post of implement K-means is Implement kernel K-means This post is written about kernel K-means. The kernel K-means is the method which covers the weak point of K-means. I will explain kernel K-means another post. Overview 1 of K coding scheme prototype vector Distortion measure Computing 1 of K coding scheme K-means algorithm is clustering K of class. Now, K-means algorithm expresses that $x_n$ be belong to k like as follows. Let vector $r_n:1 \times K$ is $$r_n := (0,0,..,1,..,0)$$ this vector have 1 in element of k'th and have 0 in else. This expression is called 1 of K coding scheme. Prototype vector K-means algorithm chooses vector which called a prototype. this vector has represented by the cluster. K-means algorithm is regard mean vector as representative of the cluster. I...

最尤推定

Introduction English ver 今日は最尤推定について加工と思います。これは統計的推定でよく使われる手法です。最尤推定の例も書こうと思います。初めに尤度の説明をし、そのあとで最尤推定の説明をします。 概要 尤度 最尤推定 最尤推定の問題点 尤度 前提条件から得られる観察データを考えます。この時、えられた観測データに対して前提条件が尤もらしい条件であるかの値を尤度といいます。 なにをゆっているのかわからない人がほとんどだと思います。。。 尤度の例を扱っていきます。 コインを投げることを考えます。このコインは確率Pで表、確率1-Pで裏を出すコインだとします。 例えば、100回コインを投げたとき、全て表だったとします。この時このコインが表が出る確率はかなり1に近いことが予想されます。 ではもし、表が出る確率PがP=0.5だとします。この時、表が100回連続で出る確率は$0.5^{100} = 7.88860e-31$になります。あり得ない確率ですね。これがP=0.5としたときのもっともらしさです。つまり、あまり現実的ではないということです。 もしP=0.99とするとき、100回とも表が出る確率は$0.99^{100} = 0.3666....$となります。つまり、P=0.99としたときの尤度は0.36くらいということです。よって、P=0.5よりかは現実見があることになります。まだまだ低い数字ではありますが。 観測データである、100回表が出るという事象を固定したとき、尤度はPを変数としたP(100回表|P)を尤度関数と呼びます。この関数の値を尤度と呼びます。 尤度が高いほうが尤もらしい値、つまり理にかなっているなと感じることができる値ということになります。 例えば、先ほどの例でゆうと、 P=0.5としたときの尤度は7.88860e-31でした。P=0.99としたときの尤度は0.3666でした。よってP=0.5より、P=0.99のほうが尤もらしい自然な値ということになります。 最尤推定 最尤推定とは得られた観測データからデータが依存している分布のパラメーターを推測するための手法です。 最尤推定では尤度を最大化して、最も尤もらしいパラメーターを求めます。 確率密度関数...