回答編集履歴

2

Update

2021/12/22 00:29

投稿

melian
melian

スコア20655

test CHANGED
@@ -1,6 +1,8 @@
1
1
  ```python
2
2
 
3
3
  from collections import Counter
4
+
5
+ import math
4
6
 
5
7
 
6
8
 
@@ -20,20 +22,34 @@
20
22
 
21
23
 
22
24
 
23
- from pprint import pprint
25
+ num = len(ni)
24
26
 
27
+ pattern = 'ACDEFGHIKLMNPQRSTVWY-'
28
+
25
- pprint(ni)
29
+ for i in range(num):
30
+
31
+ shannon = []
32
+
33
+ for j in pattern:
34
+
35
+ pi = ni[i][j]/num
36
+
37
+ if pi > 0:
38
+
39
+ shannon.append(float(pi*math.log(pi, 2)))
40
+
41
+ print(f'{i}: {-sum(shannon)}')
26
42
 
27
43
 
28
44
 
29
45
  #
30
46
 
31
- [Counter({'A': 4}),
47
+ 0: -0.0
32
48
 
33
- Counter({'C': 3, 'D': 1}),
49
+ 1: 0.8112781244591328
34
50
 
35
- Counter({'D': 2, 'E': 2}),
51
+ 2: 1.0
36
52
 
37
- Counter({'E': 1, 'D': 1, 'C': 1, 'A': 1})]
53
+ 3: 2.0
38
54
 
39
55
  ```

1

Update

2021/12/22 00:29

投稿

melian
melian

スコア20655

test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
 
18
18
 
19
- ni = [Counter(c) for c in map(list, zip(*q))]
19
+ ni = list(map(Counter, map(list, zip(*q))))
20
20
 
21
21
 
22
22