From 11b7625d48a0204910bfcb6b7798202d2ff57d13 Mon Sep 17 00:00:00 2001 From: Luo Tao Date: Mon, 5 Dec 2016 19:10:34 +0800 Subject: [PATCH] use dictionary comprehension to pythonic code --- demo/seqToseq/dataprovider.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/demo/seqToseq/dataprovider.py b/demo/seqToseq/dataprovider.py index 0614d54aaf8..5174092df26 100755 --- a/demo/seqToseq/dataprovider.py +++ b/demo/seqToseq/dataprovider.py @@ -25,11 +25,17 @@ def hook(settings, src_dict_path, trg_dict_path, is_generating, file_list, # job_mode = 0: generating mode settings.job_mode = not is_generating settings.src_dict = dict() - for line_count, line in enumerate(open(src_dict_path, "r")): - settings.src_dict[line.strip()] = line_count + with open(src_dict_path, "r") as fin: + settings.src_dict = { + line.strip(): line_count + for line_count, line in enumerate(fin) + } settings.trg_dict = dict() - for line_count, line in enumerate(open(trg_dict_path, "r")): - settings.trg_dict[line.strip()] = line_count + with open(trg_dict_path, "r") as fin: + settings.trg_dict = { + line.strip(): line_count + for line_count, line in enumerate(fin) + } settings.logger.info("src dict len : %d" % (len(settings.src_dict))) settings.sample_count = 0 -- GitLab