Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented Nov 25, 2016

100本ノック11修正及び13

# -*- coding: utf-8 -*-
line_number = 0
with open('input.txt') as input_file:
for line in input_file:
Copy link

Choose a reason for hiding this comment

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

line_numberを使うよりもenumerateを使う

for num, line in enumerate(input_file):
    # hogehoge

@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
with open('input.txt') as input_file:
Copy link

Choose a reason for hiding this comment

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

ネストが多くなるのは、できるだけ避ける
with構文の場合は, 複数の構文も下の様に書ける

with open('file1') as fp1, open('file2') as fp2:
   #hogehoge 

with open('col1.txt', 'w') as output1_file:
with open('col2.txt', 'w') as output2_file:
for line in iter(input_file.readlines()):
output1_file.writelines(line.split('\t')[0]+'\n')
Copy link

Choose a reason for hiding this comment

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

3個以上になると \を使って改行しないといけないので ならcontextlib.ExitStackが個人的にはオススメ
ExitStack

@takegue
Copy link

takegue commented Nov 26, 2016

あ、あとPR先をmaruyama branchに変えておいてくださいお願いします!

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