Skip to content
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

Open
wants to merge 9 commits into
base: inaoka
Choose a base branch
from
Open

q13, q14, q15, q16 を解答 #31

wants to merge 9 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Nov 24, 2016

No description provided.

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"),
Copy link

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"),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rstrip() だけで改行等を削除してくれます。

@ghost ghost changed the title q13 を解答 q13, q14, q15 を解答 Nov 25, 2016
path = args[2]

with open(path, "r") as fin:
print(*deque(fin, n), sep="", end="")

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これくらい単純なプログラムはsys.argvでも良いと思います。
高機能なargparseが使えるようになると、言語処理では結構できることが広がるかと

Copy link

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)
Copy link

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):
Copy link

@takegue takegue Nov 26, 2016

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='')

@ghost ghost changed the title q13, q14, q15 を解答 q13, q14, q15, q16 を解答 Dec 2, 2016
@ghost
Copy link
Author

ghost commented Dec 2, 2016

q16 も解答しました

n = int(args[1])
path = args[2]
with open(path, "r") as f:
total_line_num = count_file_lines(f)
Copy link

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):
Copy link

@takegue takegue Dec 2, 2016

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で消しています

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants