回答編集履歴
3
重複対策
answer
CHANGED
@@ -33,6 +33,6 @@
|
|
33
33
|
next(IN)
|
34
34
|
counter = ChainMap(Counter(map(str.strip, IN)), defaultdict(int))
|
35
35
|
print("年月日,カウント", file=OUT)
|
36
|
-
for key in sorted(chain(counter, ["2020/1/4"])):
|
36
|
+
for key in sorted(set(chain(counter, ["2020/1/4"]))):
|
37
37
|
print(key, counter[key], sep=",", file=OUT)
|
38
38
|
```
|
2
存在しない日付
answer
CHANGED
@@ -17,4 +17,22 @@
|
|
17
17
|
for key, count in sorted(Counter(map(str.strip, IN)).items()):
|
18
18
|
print(key, count, sep=",", file=OUT)
|
19
19
|
|
20
|
+
```
|
21
|
+
|
22
|
+
## 追記:存在しない日付
|
23
|
+
|
24
|
+
ここまでくると無理やり感が。。。存在しない日付等、高度なことをしたければPandasの方がシンプルに書ける。
|
25
|
+
|
26
|
+
```python
|
27
|
+
from collections import ChainMap
|
28
|
+
from collections import Counter
|
29
|
+
from collections import defaultdict
|
30
|
+
from itertools import chain
|
31
|
+
|
32
|
+
with open("dates.csv") as IN, open("count.csv", "w") as OUT:
|
33
|
+
next(IN)
|
34
|
+
counter = ChainMap(Counter(map(str.strip, IN)), defaultdict(int))
|
35
|
+
print("年月日,カウント", file=OUT)
|
36
|
+
for key in sorted(chain(counter, ["2020/1/4"])):
|
37
|
+
print(key, counter[key], sep=",", file=OUT)
|
20
38
|
```
|
1
ヘッダ対応版
answer
CHANGED
@@ -4,4 +4,17 @@
|
|
4
4
|
with open("dates.csv") as IN, open("count.csv", "w") as OUT:
|
5
5
|
for key, count in sorted(Counter(map(str.strip, IN)).items()):
|
6
6
|
print(key, count, sep=",", file=OUT)
|
7
|
+
```
|
8
|
+
|
9
|
+
## 追記:以下、ヘッダ対応版
|
10
|
+
|
11
|
+
```python
|
12
|
+
from collections import Counter
|
13
|
+
|
14
|
+
with open("dates.csv") as IN, open("count.csv", "w") as OUT:
|
15
|
+
next(IN)
|
16
|
+
print("年月日,カウント", file=OUT)
|
17
|
+
for key, count in sorted(Counter(map(str.strip, IN)).items()):
|
18
|
+
print(key, count, sep=",", file=OUT)
|
19
|
+
|
7
20
|
```
|