Startup Data scientist Blog

データ分析系のテック情報を発信します

2022-01-13から1日間の記事一覧

python 再帰呼出し n*fact(n-1)

pythonの再帰呼出しにより階乗 n! の計算が可能になります。 factorialコード def fact(n): if (n <= 1): return 1 else: return n * fact(n - 1) print(fact(5)) Output >> 120

python inputを使った簡単なコード

python inputを使った文字列の入力 prefix = "Hello " n1 = input("Enter your name ") n2 = input("Enter your another name ") res = prefix + n1 + " and " + n2 print(res) Output >> Enter your name mikeEnter your another name tarouHello mike and …

python for elseのループコード

Pythonではwhile, forのループにelseを使用することが出来ます。他の言語と比較すると珍しい機能で、ありがちなケースでは「ループ処理で何かを探索して見つけたらbreakする、breakしなかったら見つからなかった」といったケースでフラグ変数を使う必要がな…

pythonの挿入ソート(insertion sort)

シンプルで直感的なソートアルゴリズム def InsertionSort(A): for j in range(1, len(A)): key = A[j] i = j - 1 while (i >= 0) and (A[i] > key): i = i - 1 A[i+1] = key input = [8, 3, 9, 15, 29, 7, 10] InsertionSort(input) print(input) Output >>…

Pythonで簡単な形態素解析

tokenize python input = 'John, Doe, 1983, 4, 29, male' tokens = input.split(',') firstName = tokens[0] lastName = tokens[1] birthdate = (int(tokens[2]), int(tokens[3]), int(tokens[4])) isMale = (tokens[5] == 'male') print('Hi ' + firstName…

python forとifを使ったフィルター処理

python forとifを使った簡単なフィルター処理 input = [("Mary", 27), ("Joe", 30), ("Ruth", 43), ("Boe", 17),("Jenny", 22)] youngPeople = [] for (person, age) in input: if age < 30: youngPeople.append(person) else: print(person + 'is too old!'…

python stringsとnumberの基本的なコード

数字型 #number age = 99 pi = 3.14 print(age) print(pi) Output >> 993.14 文字列 #strings s = 'Rutherford Birchard Hayas' tokens = s.split() firstName = tokens[0] middleName = tokens[1] lastName = tokens[2] s2 = firstName + " " + middleName …