Skip to content

Instantly share code, notes, and snippets.

@boronology
Last active September 15, 2016 11:41
Show Gist options
  • Save boronology/73714bf39c674d0b845e0b966cbfc81d to your computer and use it in GitHub Desktop.
Save boronology/73714bf39c674d0b845e0b966cbfc81d to your computer and use it in GitHub Desktop.
言語処理100本ノック http://www.cl.ecei.tohoku.ac.jp/nlp100/
#10. 行数のカウント
c=0
with open("hightemp.txt") as f:
for i in f:
c += 1
print(c)
#別解
with open("hightemp.txt") as f:
print(len(list(f)))
#11. タブをスペースに置換
with open("hightemp.txt") as f:
for i in f:
print(i.replace("\t", " "))
#12. 1列目をcol1.txtに,2列目をcol2.txtに保存
with open("hightemp.txt") as f:
for i in f:
t = i.split()
with open("col1.txt","a") as col1, open ("col2.txt","a") as col2:
col1.write(t[0]+"\n")
col2.write(t[1]+"\n")
#13. col1.txtとcol2.txtをマージ
with open("merged.txt","a") as m:
with open("col1.txt") as col1, open ("col2.txt") as col2:
col1_list = list(col1)
col2_list = list(col2)
for i in range(len(col1_list)):
m.write(col1_list[i].replace("\n","") + "\t" + col2_list[i])
#14. 先頭からN行を出力
n = int(input())
with open("hightemp.txt") as f:
l = list(f)
for i in range(n):
print(l[i])
#15. 末尾のN行を出力
n = int(input())
with open("hightemp.txt") as f:
l = list(f)
for i in l[-n:]:
print(i)
#16. ファイルをN分割する
n = int(input())
with open("hightemp.txt","r") as f:
t = list(f)
d = len(t) // n
for i in range(len(t)):
with open("split_" + str(i // d),"a") as s:
s.write(t[i])
#17. 1列目の文字列の異なり
with open("hightemp.txt") as f:
s = set([])
for i in f:
s.add(i[0])
print(len(s))
#18. 各行を3コラム目の数値の降順にソート
with open("hightemp.txt") as f:
l = list(map(lambda x:x.split(),list(f)))
l.sort(key=lambda x:x[2])
print(l)
#19. 各行の1コラム目の文字列の出現頻度を求め,出現頻度の高い順に並べる
with open("hightemp.txt") as f:
d = {}
for i in f:
t = i.split()
if t[0] in d.keys():
d[t[0]]+=1
else:
d[t[0]] = 1
l = sorted(list(d.items()),key=lambda x:x[1],reverse=1)
print(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment