teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

コード修正

2018/04/11 06:57

投稿

arch_
arch_

スコア158

answer CHANGED
@@ -1,6 +1,6 @@
1
1
  reでANDマッチングしてみました。
2
2
 
3
- ``` python
3
+ ```Python
4
4
  import re
5
5
  from itertools import combinations
6
6
 
@@ -23,12 +23,12 @@
23
23
  return _match
24
24
 
25
25
 
26
- def count_charset(charset, words):
26
+ def count_matching(charset, words):
27
27
  return (charset, len(list(filter(match_andpattern(charset), words))))
28
28
 
29
29
 
30
30
  words = ["あいう", "あうえ", "い", "うい"]
31
31
  charset_list = make_charsets(words)
32
- count_list = [count_charset(charset, words) for charset in charset_list]
32
+ count_list = [count_matching(charset, words) for charset in charset_list]
33
33
  after = list(map(print, filter(lambda x: x[1] != 0, count_list)))
34
34
  ```

1

修正

2018/04/11 06:57

投稿

arch_
arch_

スコア158

answer CHANGED
@@ -1,1 +1,34 @@
1
- reでANDまっちんか
1
+ reでANDマッチングしてみした。
2
+
3
+ ``` python
4
+ import re
5
+ from itertools import combinations
6
+
7
+
8
+ def make_charsets(words):
9
+ chars = sorted(set(''.join(words)))
10
+ charset_map = [
11
+ list(combinations(chars, i))
12
+ for i in range(1, len(chars) + 1)
13
+ ]
14
+ return [charset for line in charset_map for charset in line]
15
+
16
+
17
+ def match_andpattern(charset):
18
+ andpattern = '^' + ''.join(['(?=.*' + c + ')' for c in charset])
19
+
20
+ def _match(word):
21
+ return re.match(andpattern, word) != None
22
+
23
+ return _match
24
+
25
+
26
+ def count_charset(charset, words):
27
+ return (charset, len(list(filter(match_andpattern(charset), words))))
28
+
29
+
30
+ words = ["あいう", "あうえ", "い", "うい"]
31
+ charset_list = make_charsets(words)
32
+ count_list = [count_charset(charset, words) for charset in charset_list]
33
+ after = list(map(print, filter(lambda x: x[1] != 0, count_list)))
34
+ ```