回答編集履歴

1

ソースを変更。別解を追加

2019/03/14 14:53

投稿

退会済みユーザー
test CHANGED
@@ -12,14 +12,38 @@
12
12
 
13
13
  data.append(rows[0][0])
14
14
 
15
- for x,y in zip(rows[:-1],rows[1:]):
15
+ for (_,_,e),(h,s,_) in zip(rows[:-1],rows[1:]): # for x,y in zip(rows[:-1],rows[1:]):
16
16
 
17
- data.append(("-" if x[2] < y[1] else "\n"))
17
+ data.append(("-" if e < s else "\n")) # data.append(("-" if x[2] < y[1] else "\n"))
18
18
 
19
- data.append(y[0])
19
+ data.append(h) # data.append(y[0])
20
20
 
21
21
  else:
22
22
 
23
23
  print(''.join(data))
24
24
 
25
25
  ```
26
+
27
+ 上記のforの変数名を変更しました。x,y --> (_,_,e),(h,s,_)
28
+
29
+
30
+
31
+ **別解**
32
+
33
+ functools.reduceを使うと短くなりました。pythonらしくないかもしれません。
34
+
35
+
36
+
37
+ ```python
38
+
39
+ import functools
40
+
41
+ with open("data", "r") as f:
42
+
43
+ rows = list(map(lambda x: (x[1],int(x[2]),int(x[3])),[(l.split()) for l in f if l.startswith("list")]))
44
+
45
+ (h,_,_)=functools.reduce(lambda x,y: (x[0] + ("-" if x[2] < y[1] else "\n") + y[0] ,y[1] ,y[2]), rows)
46
+
47
+ print(h)
48
+
49
+ ```