time() を利用して素数を探す時間を計測してみましょう.1000000と1001000の間にある素数を探しますが,その際に素数が見つかるたびに時間を記録することとします.素数と時間のペアを1行ごとに保存しましょう.
これまでは最初からリストになったものを保存していましたが,今回は自分でリストにして保存します.
先週の演習問題 iv. で時間の取得 time() を体験しました.それを使って,同じように経過時間を記録します.
6月11日の宿題で測定値風のデータを作るモジュールを利用しました.今度は,そのデータの1行目に見出しとしての x と y という文字をつけて CSV ファイルとして保存するプログラムを作成してみましょう.
今回は特にモジュールにしないで,def でスクリプト内に関数としてデータ作成部分を入れています.
そして,書き込む際にまずヘッダを writerow で書き込んで,その後で writerows でデータを保存しています.
先ほど保存した CSV データを読み込んで,近似式を計算するプログラムを作成しましょう.
x y 0.859 4.876 2.087 7.761 2.986 11.392 4.079 16.091 5.022 20.190 5.972 23.870 6.968 26.639 8.163 31.753 8.898 36.134 9.769 39.528 Fitted line: y = 0.138 + 3.957 x |
実験などでは条件を変えて測定値を順次取得することはよく行われていますが,特定の条件のときにはデータが無い場合などもあります.その際に途中のデータが抜けたままのリストを扱うこともあります.
例えば,以下のようなデータがあるとき,そのデータを読み込んで空の部分を削除し,平均値を求めるプログラムを作成しましょう.
[['12', '', '34', '56', '', '78']] [12, 34, 56, 78] Mean of above list: 45 |
読み込んだデータを文字列データから数値データに変換する際にデータとして存在するものだけを選択するために,以下のような操作を行ってみました.
lst = [int(txtdata[0][i]) for i in range(6) if txtdata[0][i]]
ここまで CSV 形式のデータの読み込みだけを行ってきましたが,教科書の p.359 にテキストデータの読み込みも紹介されていますので,最後に挑戦してみましょう.
日本国憲法の前文(英語版)を用意しましたので,それが読み込んで表示できるか,確認しましょう.
THE CONSTITUTION OF JAPAN We, the Japanese people, acting through our duly elected representatives in the National Diet, determined that we shall secure for ourselves and our posterity the fruits of peaceful cooperation with all nations and the blessings of liberty throughout this land, and resolved that never again shall we be visited with the horrors of war through the action of government, do proclaim that sovereign power resides with the people and do firmly establish this Constitution. Government is a sacred trust of the people, the authority for which derived from the people, the powers of which are exercised by the representatives of the people, and the benefits of which are enjoyed by the people. This is a universal principle of mankind upon which this Constitution is founded. We reject and revoke all constitutions, laws, ordinances and the prescripts in conflict herewith. We, the Japanese people, desire peace for all time and are deeply conscious of the high ideals controlling human relationship, and we have determined to preserve our security and existence, trusting in the justice and faith of the peace-loving peoples of the world. We desire to occupy an honored place in an international society striving for the preservation of peace, and the banishment of tyranny and slavery, oppression and intolerance for all time from the earth. We recognize that all peoples of the world have the right to live in peace, free from fear and want. We believe that no nation is responsible to itself alone, but that laws of political morality are universal; and that obedience to such laws is incumbent upon all nations who would sustain their own sovereignty and justify their sovereign relationship with other nations. We, Japanese people, pledge our national honor to accomplish these high ideals and purposes with all our resources. |
せっかくなので,読み込んだ憲法前文をリストに収めましょう.全部で5行の文章となっていますので,以下のように文字列リストに収まることになります.
['THE CONSTITUTION OF JAPAN\n', '\n', 'We, the Japanese people, acting through our duly elected representatives in the National Diet, determined that we shall secure for ourselves and our posterity the fruits of peaceful cooperation with all nations and the blessings of liberty throughout this land, and resolved that never again shall we be visited with the horrors of war through the action of government, do proclaim that sovereign power resides with the people and do firmly establish this Constitution. Government is a sacred trust of the people, the authority for which derived from the people, the powers of which are exercised by the representatives of the people, and the benefits of which are enjoyed by the people. This is a universal principle of mankind upon which this Constitution is founded. We reject and revoke all constitutions, laws, ordinances and the prescripts in conflict herewith.\n', '\n', 'We, the Japanese people, desire peace for all time and are deeply conscious of the high ideals controlling human relationship, and we have determined to preserve our security and existence, trusting in the justice and faith of the peace-loving peoples of the world. We desire to occupy an honored place in an international society striving for the preservation of peace, and the banishment of tyranny and slavery, oppression and intolerance for all time from the earth. We recognize that all peoples of the world have the right to live in peace, free from fear and want.\n', '\n', 'We believe that no nation is responsible to itself alone, but that laws of political morality are universal; and that obedience to such laws is incumbent upon all nations who would sustain their own sovereignty and justify their sovereign relationship with other nations.\n', '\n', 'We, Japanese people, pledge our national honor to accomplish these high ideals and purposes with all our resources.'] |
最初に空のリストを作っておいて,行を読み込むたびに append する形にしました.
ついでに,設問 iv. を参考に上のリストから改行文字 \n を消しておきましょう.
['THE CONSTITUTION OF JAPAN', 'We, the Japanese people, acting through our duly elected representatives in the National Diet, determined that we shall secure for ourselves and our posterity the fruits of peaceful cooperation with all nations and the blessings of liberty throughout this land, and resolved that never again shall we be visited with the horrors of war through the action of government, do proclaim that sovereign power resides with the people and do firmly establish this Constitution. Government is a sacred trust of the people, the authority for which derived from the people, the powers of which are exercised by the representatives of the people, and the benefits of which are enjoyed by the people. This is a universal principle of mankind upon which this Constitution is founded. We reject and revoke all constitutions, laws, ordinances and the prescripts in conflict herewith.', 'We, the Japanese people, desire peace for all time and are deeply conscious of the high ideals controlling human relationship, and we have determined to preserve our security and existence, trusting in the justice and faith of the peace-loving peoples of the world. We desire to occupy an honored place in an international society striving for the preservation of peace, and the banishment of tyranny and slavery, oppression and intolerance for all time from the earth. We recognize that all peoples of the world have the right to live in peace, free from fear and want.', 'We believe that no nation is responsible to itself alone, but that laws of political morality are universal; and that obedience to such laws is incumbent upon all nations who would sustain their own sovereignty and justify their sovereign relationship with other nations.', 'We, Japanese people, pledge our national honor to accomplish these high ideals and purposes with all our resources.'] |
replace メソッドを map 関数とラムダ式と組み合わせて改行文字を削除します.また,空文字列も作られてしまうので,iv. と同じように削除します.