-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
q13, q14, q15, q16 を解答 #31
base: inaoka
Are you sure you want to change the base?
Conversation
open("col2.txt", "r") as col2_f,\ | ||
open("col12.txt", "w") as col12_f: | ||
for col1, col2 in zip(col1_f, col2_f): | ||
print("{}\t{}".format(col1.rstrip("\r\n"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rstrip() だけでいい
open("col2.txt", "r") as col2_f,\ | ||
open("col12.txt", "w") as col12_f: | ||
for col1, col2 in zip(col1_f, col2_f): | ||
print("{}\t{}".format(col1.rstrip("\r\n"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rstrip() だけで改行等を削除してくれます。
path = args[2] | ||
|
||
with open(path, "r") as fin: | ||
print(*deque(fin, n), sep="", end="") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このdequeの使い方いいね:+1:
import sys | ||
from collections import deque | ||
|
||
args = sys.argv |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これくらい単純なプログラムはsys.argv
でも良いと思います。
高機能なargparse
が使えるようになると、言語処理では結構できることが広がるかと
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
外部ライブラリを何かライブラリを使うなら clickってやつがオススメです.
http://click.pocoo.org/5/
open("col2.txt", "r") as col2_f,\ | ||
open("col12.txt", "w") as col12_f: | ||
for col1, col2 in zip(col1_f, col2_f): | ||
print("{}\t{}".format(col1.rstrip(), col2.rstrip()), file=col12_f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print(col1.rstrip(), col2.rstrip(), file=col12_f, sep='\t')
の方がコードが読みやすくなりますね
path = args[2] | ||
|
||
with open(path, "r") as fin: | ||
for i in range(n): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readlineよりもenumerateとfileのイテレータを活用しよう
for idx, line in enumerate(fin):
if idx > n : break
print(line, end='')
q16 も解答しました |
n = int(args[1]) | ||
path = args[2] | ||
with open(path, "r") as f: | ||
total_line_num = count_file_lines(f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
行数数えるときはジェネレータ式を使ってシアみたいな書き方すると見やすいです。
n_lines = sum(1 for _ in open('file'))
with open(path, "r") as f: | ||
total_line_num = count_file_lines(f) | ||
l = devide_num(total_line_num, n) | ||
for i, line_num in enumerate(l): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
N分割するための処理ですが, 以下のような書き方もできますね.
from itertools import zip_longest
def grouper(iterable, n)
it = [iter(iterable)] * n
return zip_longest(*it, fillvalue='')
# M行(=math.ceil(n_lines / N) )にまとめる処理はこっちの方が書きやすいですね
for idx, lines in grouper(file, n_lines // N):
with open('{}.{}'.format(path, idx), 'w') as fout:
print( ''.join(lines).strip(), file=fout)
from itertools import groupby
# N分割する処理はこっちの方が書きやすいですね
for idx, lines in groupby(enumerate(file), key=lambda idx, line: idx//N):
with open('{}.{}'.format(path, idx), 'w') as fout:
print(''.join(lines).strip(), file=fout) #連続する改行をstripで消しています
No description provided.