In [19]:
#-*- coding:utf-8 -*-

11. タブをスペースに置換

タブ1文字につきスペース1文字に置換せよ.確認にはsedコマンド,trコマンド,もしくはexpandコマンドを用いよ.


In [50]:
hightemp = "".join(map(str, [i.replace('\t', ' ') for i in open('hightemp.txt', 'r')]))
print(hightemp)


高知県 江川崎 41 2013-08-12
埼玉県 熊谷 40.9 2007-08-16
岐阜県 多治見 40.9 2007-08-16
山形県 山形 40.8 1933-07-25
山梨県 甲府 40.7 2013-08-10
和歌山県 かつらぎ 40.6 1994-08-08
静岡県 天竜 40.6 1994-08-04
山梨県 勝沼 40.5 2013-08-10
埼玉県 越谷 40.4 2007-08-16
群馬県 館林 40.3 2007-08-16
群馬県 上里見 40.3 1998-07-04
愛知県 愛西 40.3 1994-08-05
千葉県 牛久 40.2 2004-07-20
静岡県 佐久間 40.2 2001-07-24
愛媛県 宇和島 40.2 1927-07-22
山形県 酒田 40.1 1978-08-03
岐阜県 美濃 40 2007-08-16
群馬県 前橋 40 2001-07-24
千葉県 茂原 39.9 2013-08-11
埼玉県 鳩山 39.9 1997-07-05
大阪府 豊中 39.9 1994-08-08
山梨県 大月 39.9 1990-07-19
山形県 鶴岡 39.9 1978-08-03
愛知県 名古屋 39.9 1942-08-02

12. 1列目をcol1.txtに,2列目をcol2.txtに保存

各行の1列目だけを抜き出したものをcol1.txtに,
2列目だけを抜き出したものをcol2.txtとしてファイルに保存せよ.
確認にはcutコマンドを用いよ.


In [53]:
col1 = open('col1.txt', 'w')
col2 = open('col2.txt', 'w')
hightemp = [i.replace('\t', ' ').split() for i in open('hightemp.txt', 'r')]

col1.write("\n".join(map(str, [i[0] for i in hightemp])))
col1.close()
col2.write("\n".join(map(str, [i[1] for i in hightemp])))
col2.close()

13. col1.txtとcol2.txtをマージ

12で作ったcol1.txtとcol2.txtを結合し,
元のファイルの1列目と2列目をタブ区切りで並べたテキストファイルを作成せよ.
確認にはpasteコマンドを用いよ.


In [13]:
%%timeit
col3 = open('col3.txt', 'w')
f1 = [i for i in open('col1.txt', 'r')]
f2 = [i for i in open('col2.txt', 'r')]
# [col3.write(i+'\t'+j) for i, j in zip(f1, f2)]
col3.close()


The slowest run took 7.39 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 141 µs per loop

14. 先頭からN行を出力

自然数Nをコマンドライン引数などの手段で受け取り,
入力のうち先頭のN行だけを表示せよ.
確認にはheadコマンドを用いよ.


In [55]:
def display_nline(n, filename):
    return "".join(map(str, [i for i in  open(filename, 'r')][:n]))

display = display_nline(5, "col3.txt")
print(display)


高知県 江川崎
埼玉県 熊谷
岐阜県 多治見
山形県 山形
山梨県 甲府

15. 末尾のN行を出力

自然数Nをコマンドライン引数などの手段で受け取り, 入力のうちの末尾のN行だけを表示せよ.
確認にはtailコマンドを用いよ.


In [85]:
def display_back_nline(n, filename):
    return "".join(map(str, [i for i in open(filename, 'r')][-n-1:-1]))

display = display_back_nline(5, "col3.txt")
print(display)


千葉県 茂原
埼玉県 鳩山
大阪府 豊中
山梨県 大月
山形県 鶴岡

16. ファイルをN分割する

自然数Nをコマンドライン引数などの手段で受け取り, 入力のファイルを行単位でN分割せよ.
同様の処理をsplitコマンドで実現せよ.


In [6]:
%%timeit
def split_file(n, filename):
    line = [i.strip('\n') for i in open(filename, 'r')]
    length = len(line)
    n = length//n
    return [line[i:i+n] for i in range(0, length, n)]

split_file(2, "col1.txt")


The slowest run took 4.83 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 57 µs per loop

17. 1列目の文字列の異なり

1列目の文字列の種類(異なる文字列の集合)を求めよ.
確認にはsort, uniqコマンドを用いよ.


In [19]:
def first_char(filename):
    return set(i[0] for i in open(filename, 'r'))

print(first_char('hightemp.txt'))


{'大', '岐', '群', '愛', '山', '和', '千', '埼', '静', '高'}

18. 各行を3コラム目の数値の降順にソート

各行を3コラム目の数値の逆順で整列せよ (注意:各行の内容は変更せずに並び替えよ). 確認にはsortコマンドを用いよ(この問題はコマンドで実行した結果とあわなくても良い).


In [10]:
%%timeit
from operator import itemgetter

def column_sort(sort_key, filename):
    return sorted([i.split() for i in open(filename, 'r')], key=itemgetter(sort_key-1))
column_sort(3, 'hightemp.txt')


10000 loops, best of 3: 85.3 µs per loop

19. 各行の1コラム目の文字列の出現頻度を求め, 出現頻度の高い順に並べる

各行の1列目の文字列の出現頻度を求め, その高い順に並べて表示せよ.
確認にはcut, uniq, sortコマンド


In [15]:
%%timeit 
from operator import itemgetter

def frequency(filename):
    first_char = [i[0] for i in open(filename, 'r')]
    dictionary = set([(i, first_char.count(i)) for i in first_char])
    return sorted(dictionary, key=itemgetter(1), reverse=True)

frequency('hightemp.txt')


10000 loops, best of 3: 76.9 µs per loop

In [16]:
%%timeit 
from operator import itemgetter
first_char = [i[0] for i in open('hightemp.txt', 'r')]
dictionary = set([(i, first_char.count(i)) for i in first_char])
sorted(dictionary, key=itemgetter(1), reverse=True)


10000 loops, best of 3: 76.6 µs per loop

In [ ]: