回答編集履歴
5
追記
answer
CHANGED
|
@@ -116,4 +116,30 @@
|
|
|
116
116
|
K 2
|
|
117
117
|
H 2
|
|
118
118
|
Y 2
|
|
119
|
-
```
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
コメントを受けて
|
|
122
|
+
---
|
|
123
|
+
> date[-1][-1] += len(line)の[-1][-1]の意味
|
|
124
|
+
|
|
125
|
+
data は、リストを要素に持つリストです。
|
|
126
|
+
まずdataの末尾のリストを取り出し、その結果の末尾の要素を取り出しています。
|
|
127
|
+
```Python
|
|
128
|
+
>>> tmp = [[11, 12], [21, 22], [31, 32]]
|
|
129
|
+
>>> print(tmp[-1])
|
|
130
|
+
[31, 32]
|
|
131
|
+
>>> print(tmp[-1][-1])
|
|
132
|
+
32
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
> 最後の for datum in date: print(*datum) がわからない
|
|
136
|
+
|
|
137
|
+
この例に限っては、次のように書くのと同じです。
|
|
138
|
+
```Python
|
|
139
|
+
for datum in data:
|
|
140
|
+
for e in datum:
|
|
141
|
+
print(e)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
なぜこのように書き換えられるか知りたいならこちらをどうぞ。
|
|
145
|
+
[Qiita - Python3.xのアスタリスク逆引き](https://qiita.com/LouiS0616/items/1bbe0a9bb93054f6c380#%E9%96%A2%E6%95%B0%E3%82%92%E5%91%BC%E3%81%B3%E5%87%BA%E3%81%99%E3%81%A8%E3%81%8D)
|
4
修正
answer
CHANGED
|
@@ -53,21 +53,24 @@
|
|
|
53
53
|
continue
|
|
54
54
|
|
|
55
55
|
if line.startswith('>'):
|
|
56
|
-
data.append([line,
|
|
56
|
+
data.append([line, Counter()])
|
|
57
57
|
else:
|
|
58
|
-
data[-1][-1]
|
|
58
|
+
data[-1][-1].update(line)
|
|
59
59
|
|
|
60
|
-
for tag,
|
|
60
|
+
for tag, counter in data:
|
|
61
61
|
print(tag)
|
|
62
62
|
|
|
63
|
-
print(
|
|
63
|
+
print(
|
|
64
|
+
sum(1 for _ in counter.elements())
|
|
65
|
+
)
|
|
64
|
-
for k, count in
|
|
66
|
+
for k, count in counter.most_common():
|
|
65
67
|
print(k, count)
|
|
66
68
|
|
|
67
69
|
print()
|
|
70
|
+
|
|
68
71
|
```
|
|
69
72
|
|
|
70
|
-
**実行結果** [Wandbox](https://wandbox.org/permlink/
|
|
73
|
+
**実行結果** [Wandbox](https://wandbox.org/permlink/82BAo4GshvqsO6Ua)
|
|
71
74
|
```
|
|
72
75
|
>YP_009518834.1 putative uncharacterized protein YjiT [Escherichia coli str. K-12 substr. MG1655]
|
|
73
76
|
505
|
3
追記
answer
CHANGED
|
@@ -38,4 +38,79 @@
|
|
|
38
38
|
505
|
|
39
39
|
>YP_009518833.1 uncharacterized protein YtiA [Escherichia coli str. K-12 substr. MG1655]
|
|
40
40
|
85
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
少しリファクタして、さらに高級にしてみた。
|
|
45
|
+
```Python
|
|
46
|
+
from collections import Counter
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
data = []
|
|
50
|
+
with open('short.sequ.txt') as fin:
|
|
51
|
+
for line in map(str.rstrip, fin):
|
|
52
|
+
if not line:
|
|
53
|
+
continue
|
|
54
|
+
|
|
55
|
+
if line.startswith('>'):
|
|
56
|
+
data.append([line, ''])
|
|
57
|
+
else:
|
|
58
|
+
data[-1][-1] += line
|
|
59
|
+
|
|
60
|
+
for tag, line in data:
|
|
61
|
+
print(tag)
|
|
62
|
+
|
|
63
|
+
print(len(line))
|
|
64
|
+
for k, count in Counter(line).most_common():
|
|
65
|
+
print(k, count)
|
|
66
|
+
|
|
67
|
+
print()
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**実行結果** [Wandbox](https://wandbox.org/permlink/XbS7h2hBNMcXxc6L)
|
|
71
|
+
```
|
|
72
|
+
>YP_009518834.1 putative uncharacterized protein YjiT [Escherichia coli str. K-12 substr. MG1655]
|
|
73
|
+
505
|
|
74
|
+
L 50
|
|
75
|
+
S 46
|
|
76
|
+
V 37
|
|
77
|
+
R 36
|
|
78
|
+
E 35
|
|
79
|
+
G 33
|
|
80
|
+
A 31
|
|
81
|
+
T 29
|
|
82
|
+
I 27
|
|
83
|
+
D 25
|
|
84
|
+
P 24
|
|
85
|
+
Q 23
|
|
86
|
+
F 23
|
|
87
|
+
K 19
|
|
88
|
+
N 19
|
|
89
|
+
Y 14
|
|
90
|
+
C 12
|
|
91
|
+
W 10
|
|
92
|
+
H 8
|
|
93
|
+
M 4
|
|
94
|
+
|
|
95
|
+
>YP_009518833.1 uncharacterized protein YtiA [Escherichia coli str. K-12 substr. MG1655]
|
|
96
|
+
85
|
|
97
|
+
G 9
|
|
98
|
+
L 7
|
|
99
|
+
I 7
|
|
100
|
+
V 6
|
|
101
|
+
R 6
|
|
102
|
+
T 5
|
|
103
|
+
Q 5
|
|
104
|
+
P 5
|
|
105
|
+
C 5
|
|
106
|
+
E 4
|
|
107
|
+
F 4
|
|
108
|
+
S 4
|
|
109
|
+
A 4
|
|
110
|
+
D 3
|
|
111
|
+
W 3
|
|
112
|
+
M 2
|
|
113
|
+
K 2
|
|
114
|
+
H 2
|
|
115
|
+
Y 2
|
|
41
116
|
```
|
2
修正
answer
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
|
|
16
16
|
書いてみた
|
|
17
17
|
---
|
|
18
|
-
|
|
18
|
+
要件を考えれば、もっとシンプルに書けそうです。
|
|
19
19
|
```Python
|
|
20
20
|
data = []
|
|
21
21
|
with open('short.sequ.txt') as fin:
|
1
追記
answer
CHANGED
|
@@ -11,4 +11,31 @@
|
|
|
11
11
|
|
|
12
12
|
> 内包表記で[ , , , , , ]と表記
|
|
13
13
|
|
|
14
|
-
おそらく内包表記の意味を勘違いして覚えています。
|
|
14
|
+
おそらく内包表記の意味を勘違いして覚えています。
|
|
15
|
+
|
|
16
|
+
書いてみた
|
|
17
|
+
---
|
|
18
|
+
メモリを潤沢に使って良いのなら、もっとシンプルに書けます。
|
|
19
|
+
```Python
|
|
20
|
+
data = []
|
|
21
|
+
with open('short.sequ.txt') as fin:
|
|
22
|
+
for line in map(str.rstrip, fin):
|
|
23
|
+
if not line:
|
|
24
|
+
continue
|
|
25
|
+
|
|
26
|
+
if line.startswith('>'):
|
|
27
|
+
data.append([line, 0])
|
|
28
|
+
else:
|
|
29
|
+
data[-1][-1] += len(line)
|
|
30
|
+
|
|
31
|
+
for datum in data:
|
|
32
|
+
print(*datum, sep='\n')
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**実行結果** [Wandbox](https://wandbox.org/permlink/oDw1bHFZaPRhwlWo)
|
|
36
|
+
```
|
|
37
|
+
>YP_009518834.1 putative uncharacterized protein YjiT [Escherichia coli str. K-12 substr. MG1655]
|
|
38
|
+
505
|
|
39
|
+
>YP_009518833.1 uncharacterized protein YtiA [Escherichia coli str. K-12 substr. MG1655]
|
|
40
|
+
85
|
|
41
|
+
```
|