回答編集履歴

3

追記

2019/01/31 14:21

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -81,3 +81,81 @@
81
81
  「」「」
82
82
 
83
83
  ```
84
+
85
+
86
+
87
+ ---
88
+
89
+ おまけ。こんなふうに書くと、行数も表示できますね。
90
+
91
+ ```Python
92
+
93
+ from itertools import groupby, tee
94
+
95
+
96
+
97
+
98
+
99
+ def enum_groupby(iterable, condition, *, start=0):
100
+
101
+ num_of_lines = start
102
+
103
+
104
+
105
+ for tf, e1 in groupby(iterable, condition):
106
+
107
+
108
+
109
+ e1, e2 = tee(e1, 2)
110
+
111
+ yield num_of_lines, (tf, e1)
112
+
113
+
114
+
115
+ num_of_lines += sum(1 for _ in e2)
116
+
117
+
118
+
119
+
120
+
121
+ it = enum_groupby(
122
+
123
+ filter(None, temporary.splitlines()), lambda l: l == '「」',
124
+
125
+ start=1
126
+
127
+ )
128
+
129
+ for l, (tf, e) in it:
130
+
131
+ if not tf:
132
+
133
+ continue
134
+
135
+
136
+
137
+ e = list(e)
138
+
139
+ if len(e) == 1:
140
+
141
+ continue
142
+
143
+
144
+
145
+ print(f'{l:2}: {"".join(e)}')
146
+
147
+ ```
148
+
149
+
150
+
151
+ **実行結果** [Wandbox](https://wandbox.org/permlink/VlmhhQij0l5qRC48)
152
+
153
+ ```
154
+
155
+ 4: 「」「」
156
+
157
+ 12: 「」「」「」
158
+
159
+ 17: 「」「」
160
+
161
+ ```

2

追記

2019/01/31 14:21

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ```Python
4
4
 
5
- for m in re.findall(r'(?:\n「」){2,}\n', temporary):
5
+ for m in re.findall(r'(?:\n「」){2,}\n', f'\n{temporary}\n'):
6
6
 
7
7
  print(''.join(m.split()))
8
8
 
@@ -10,9 +10,23 @@
10
10
 
11
11
 
12
12
 
13
- 先頭行あるいは最終行にマッチし場合もあり得ますが
13
+ f'\n{temporary}\n' 対してマッチしるのは
14
14
 
15
+ 先頭行あるいは最終行でも漏らさずマッチするための工夫です。
16
+
17
+
18
+
15
- その場合は f'\n{temporary}\n' に対してマッチを試みれば良いように思います。
19
+ **実行結果** [Wandbox](https://wandbox.org/permlink/w3vyN0ZzPHENa6m4)
20
+
21
+ ```
22
+
23
+ 「」「」
24
+
25
+ 「」「」「」
26
+
27
+ 「」「」
28
+
29
+ ```
16
30
 
17
31
 
18
32
 
@@ -20,7 +34,7 @@
20
34
 
21
35
  ---
22
36
 
23
- 正規表現の利用に拘らないのなら、次のようにも書けます。
37
+ 正規表現の利用に拘らないのなら、[itertools.groupby](https://docs.python.jp/3/library/itertools.html#itertools.groupby)を活用します。
24
38
 
25
39
  ```Python
26
40
 
@@ -53,3 +67,17 @@
53
67
  print(''.join(e))
54
68
 
55
69
  ```
70
+
71
+
72
+
73
+ **実行結果** [Wandbox](https://wandbox.org/permlink/DvY4nNrreM3obWv6)
74
+
75
+ ```
76
+
77
+ 「」「」
78
+
79
+ 「」「」「」
80
+
81
+ 「」「」
82
+
83
+ ```

1

追記

2019/01/31 14:02

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -13,3 +13,43 @@
13
13
  先頭行あるいは最終行にマッチしない場合もあり得ますが、
14
14
 
15
15
  その場合は f'\n{temporary}\n' に対してマッチを試みれば良いように思います。
16
+
17
+
18
+
19
+ あるいは
20
+
21
+ ---
22
+
23
+ 正規表現の利用に拘らないのなら、次のようにも書けます。
24
+
25
+ ```Python
26
+
27
+ import itertools
28
+
29
+
30
+
31
+ it = itertools.groupby(
32
+
33
+ temporary.splitlines(), lambda l: l == '「」'
34
+
35
+ )
36
+
37
+ for tf, e in it:
38
+
39
+ if not tf:
40
+
41
+ continue
42
+
43
+
44
+
45
+ e = list(e)
46
+
47
+ if len(e) == 1:
48
+
49
+ continue
50
+
51
+
52
+
53
+ print(''.join(e))
54
+
55
+ ```