回答編集履歴
2
修正
test
CHANGED
@@ -48,20 +48,6 @@
|
|
48
48
|
|
49
49
|
```Python
|
50
50
|
|
51
|
-
from collections import Counter
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
def flatten_2dim(array):
|
56
|
-
|
57
|
-
return [item for sublist in array for item in sublist]
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
data = [['あ', 'い', 'あ'], ['う', 'え', 'お', 'お']]
|
62
|
-
|
63
|
-
counter = Counter(flatten_2dim(data))
|
64
|
-
|
65
51
|
for word, cnt in sorted(counter.items(), key=lambda x: x[0]):
|
66
52
|
|
67
53
|
print(word, cnt)
|
@@ -88,6 +74,20 @@
|
|
88
74
|
|
89
75
|
---
|
90
76
|
|
77
|
+
> あと、正直tupleもいまいちよく分かってません。
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
タプルを使うと、複数の値をまとめて扱うことが出来ます。
|
82
|
+
|
83
|
+
リストと似ていますが、タプルは**イミュータブル(変更不可)**なオブジェクトです。
|
84
|
+
|
85
|
+
詳しくは調べてください。
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
---
|
90
|
+
|
91
91
|
teratailでは、コードを見やすく表示する機能があります。
|
92
92
|
|
93
93
|
編集画面を開き、コードを選択した状態で<code>ボタンを押してください。
|
1
追記
test
CHANGED
@@ -37,3 +37,59 @@
|
|
37
37
|
"""
|
38
38
|
|
39
39
|
```
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
---
|
44
|
+
|
45
|
+
なお、あいうえお順で出力したい場合、most_commonは不適です。
|
46
|
+
|
47
|
+
ソートして出力すればよいでしょう。
|
48
|
+
|
49
|
+
```Python
|
50
|
+
|
51
|
+
from collections import Counter
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def flatten_2dim(array):
|
56
|
+
|
57
|
+
return [item for sublist in array for item in sublist]
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
data = [['あ', 'い', 'あ'], ['う', 'え', 'お', 'お']]
|
62
|
+
|
63
|
+
counter = Counter(flatten_2dim(data))
|
64
|
+
|
65
|
+
for word, cnt in sorted(counter.items(), key=lambda x: x[0]):
|
66
|
+
|
67
|
+
print(word, cnt)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
"""実行結果
|
72
|
+
|
73
|
+
あ 2
|
74
|
+
|
75
|
+
い 1
|
76
|
+
|
77
|
+
う 1
|
78
|
+
|
79
|
+
え 1
|
80
|
+
|
81
|
+
お 2
|
82
|
+
|
83
|
+
"""
|
84
|
+
|
85
|
+
```
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
---
|
90
|
+
|
91
|
+
teratailでは、コードを見やすく表示する機能があります。
|
92
|
+
|
93
|
+
編集画面を開き、コードを選択した状態で<code>ボタンを押してください。
|
94
|
+
|
95
|
+
特にPythonの場合、インデントが崩れるとコードの意味が変わってしまいます。
|