Python プログラミング練習 2日目
- 円周率 "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.
#! /usr/bin/env python # encoding:utf-8 elm = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics." temp = elm.split() line = [] for i in range(len(temp)): word = temp[i].rstrip(',' or '.') line.append(len(word)) print line
- 元素記号 "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."という文を単語に分解し,1, 5, 6, 7, 8, 9, 15, 16, 19番目の単語は先頭の1文字,それ以外の単語は先頭に2文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.
#! /usr/bin/env python # encoding:utf-8 # 入力 text = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can." dic = {} words_list = text.split() uniq = [1, 5, 6, 7, 8, 9, 15, 16, 19] for i in range(len(words_list)): num = i + 1 if num in uniq: dic[num] = words_list[i][0:1] else: dic[num] = words_list[i][0:2] for k in sorted(dic): print k, dic[k] print str(len(words_list))
出力結果が間違っていた場合は 後で修正
100問中、5問終了 飛ばした問題無し
Python プログラミング練習 1日目
プログラミング練習に良いサイトを見つけた このサイトの問題を継続的に解くことを目的とする
- 文字列の逆順
文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.
入力文字列:stressed
出力文字列:desserts
#! /usr/bin/env python # encoding:utf-8 # 入力文字列 elm = "stressed" # 出力 print elm[::-1]
- 「パタトクカシーー」
「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.
入力文字列:パタトクカシーー
出力文字列:パトカー
#! /usr/bin/env python # encoding:utf-8 # 入力文字列 elm = u"パタトクカシーー" # 出力 temp = elm[0:1] + elm[2:3] + elm[4:5] + elm[6:7] print temp
- 「パトカー」+「タクシー」=「パタトクカシーー」
「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.
#! /usr/bin/env python # encoding:utf-8 # 入力1 str1 = list(u"パトカー") # 入力2 str2 = list(u"タクシー") temp = '' for (i, l) in zip(str1, str2): temp += i + l print temp
Biopython 勉強開始
自分が興味のある分野としてBioinformaticsがあり、
Pythonで特にBiopythonというpackageがあるのでこれを学んでいこうと思う。
本家は以下URLから
最初なので、このBiopythonが何かをメモ。
・ Bioinformaticsで使われている様々なデータ構造のファイルをparse (構文解析) 可能
Blast output, ClustalW, FASTA, Genbank, Pubmed and Medline, ExPASy files, like Enzyme and Prosite
SCOP, including ‘dom’ and ‘lin’ files, UniGene, SwissProt
・Bioinformaticsで行われる解析について、このBiopythonであらゆることが実行可能
(詳しいことはそれぞれの章で詳しく見る予定)
ざっくり簡単に。使用されているデータ構造やツールの種類は多いので
それぞれに対応したモジュールを扱っている項目で詳しく学んでいく。
何ができるのか、今行っている作業がはかどるのか (毎回毎回似たようなコード書くのはウンザリ)
さて、中身が薄っぺらい記事になったがこれを第1回目として。
ランダムに作成したテキストファイルに共通するパターンが複数存在する場合に何種類の文字から構成されていればパターンの数が安定するか
実際に行った事柄を最初に箇条書きでまとめておく。
・random moduleで使用する文字の種類(数)を限定した上で全体の文字数を揃えたファイルを複数作成
・作成したファイル中に同じパターンが2回以上出た場合にその数を数える
まず、ランダムな文字ファイルの作成プログラム。
#! /usr/bin/env python # -*- coding:utf-8 -*- import random user_in = raw_input("What numbers do you like?: ") text = '' for i in range(int(user_in)): r = random.choice("ABCDEFGHI") text += str(r) f = open("text_" + str(user_in) + ".txt", "w") f.write(text) f.close()
choiceメソッドで取得しているアルファベット数を変更する(ユーザーの入力で文字の種類を変更するようにすればよかった)。 今回は文字の種類の数を5, 6, 7, 8, 9, 10, 15, 20として8ファイル作成した。
次に作成したファイルに対して2回以上ヒットするパターンの検出プログラム。
#! /usr/bin/env python # -*- coding:utf-8 -*- import sys temp = '' filename = sys.argv[1] inputfile = open(filename, 'r') user_in_min = raw_input("min text length: ") user_in_max = raw_input("max text length: ") text_list = {} result = '' count = 0 for line in inputfile: temp = line.rstrip() for k in range(int(user_in_min), int(user_in_max) + 1): for i in range(len(temp)): if (int(i) + int(k)) <= len(temp): if text_list.has_key(temp[i:i+k+1]): text_list[temp[i:i+k+1]] += 1 else: text_list[temp[i:i+k+1]] = 1 for u in sorted(text_list.keys(), key = lambda x: len(x)): if int(text_list[u]) >= 2: count += 1 result += str(u) + "\t" + str(text_list[u]) + "\n" result += str(count) + "\n" inputfile.close() name = str(filename).split(".txt")[0] f = open(str(name) + "_result.txt", "w") f.write(result) f.close()
パターンの長さはユーザー指定ではあるが今回全て5〜20の範囲で探した。
(user_in_min = 5, usr_in_max = 20)
結果として、
文字の種類:2回以上ヒットしたパターンの数 5:72 6:35 7:18 8:23 9:16 10:15 15:15 20:15
パターンマッチのプログラムを見ればわかるが重複を許しているので 短いマッチは長いものに含まれていることが考えられるため 重複を許容しなければもっと数が減って文字の種類がもう少し少ない段階から 2回以上ヒットするパターンの数が一定になるかもしれない。 文字の総数とパターンの長さを変更してもう少し細かく見れば 何かよくわからないが見えるかも...しれない......。
glob module
最近書いてるプログラムでよく使っているので今のうちにメモ。
正確に言えば、ディレクトリ内に存在するファイルを全件読み込むには
どうすれば良いか調べているときに最初に行き着いたmodule。
ディレクトリ内に存在するファイル名(パス名?)のリストを返す。
file1.txt file2.txt
上記のようなファイルがあるとする。
# coding: UTF-8 import glob for file in glob.glob("*.txt"): print str(file)
結果は、
file1.txt
file2.txt
となる。
普段はこんな使い方せずにファイル名の一部を取得したいので
# coding: UTF-8 import glob name = '' for file in glob.glob("*.txt"): name = str(file).split(".")[0] print name
結果は
file1
file2
となる。
あまりglob module使っているコード見ないので他の方法模索中。