回答編集履歴

5

追記

2018/12/09 11:59

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -235,3 +235,55 @@
235
235
  Y 2
236
236
 
237
237
  ```
238
+
239
+
240
+
241
+ コメントを受けて
242
+
243
+ ---
244
+
245
+ > date[-1][-1] += len(line)の[-1][-1]の意味
246
+
247
+
248
+
249
+ data は、リストを要素に持つリストです。
250
+
251
+ まずdataの末尾のリストを取り出し、その結果の末尾の要素を取り出しています。
252
+
253
+ ```Python
254
+
255
+ >>> tmp = [[11, 12], [21, 22], [31, 32]]
256
+
257
+ >>> print(tmp[-1])
258
+
259
+ [31, 32]
260
+
261
+ >>> print(tmp[-1][-1])
262
+
263
+ 32
264
+
265
+ ```
266
+
267
+
268
+
269
+ > 最後の for datum in date: print(*datum) がわからない
270
+
271
+
272
+
273
+ この例に限っては、次のように書くのと同じです。
274
+
275
+ ```Python
276
+
277
+ for datum in data:
278
+
279
+ for e in datum:
280
+
281
+ print(e)
282
+
283
+ ```
284
+
285
+
286
+
287
+ なぜこのように書き換えられるか知りたいならこちらをどうぞ。
288
+
289
+ [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

修正

2018/12/09 11:59

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -108,23 +108,27 @@
108
108
 
109
109
  if line.startswith('>'):
110
110
 
111
- data.append([line, ''])
111
+ data.append([line, Counter()])
112
112
 
113
113
  else:
114
114
 
115
- data[-1][-1] += line
115
+ data[-1][-1].update(line)
116
116
 
117
117
 
118
118
 
119
- for tag, line in data:
119
+ for tag, counter in data:
120
120
 
121
121
  print(tag)
122
122
 
123
123
 
124
124
 
125
- print(len(line))
125
+ print(
126
+
126
-
127
+ sum(1 for _ in counter.elements())
128
+
129
+ )
130
+
127
- for k, count in Counter(line).most_common():
131
+ for k, count in counter.most_common():
128
132
 
129
133
  print(k, count)
130
134
 
@@ -132,11 +136,13 @@
132
136
 
133
137
  print()
134
138
 
139
+
140
+
135
- ```
141
+ ```
136
-
137
-
138
-
142
+
143
+
144
+
139
- **実行結果** [Wandbox](https://wandbox.org/permlink/XbS7h2hBNMcXxc6L)
145
+ **実行結果** [Wandbox](https://wandbox.org/permlink/82BAo4GshvqsO6Ua)
140
146
 
141
147
  ```
142
148
 

3

追記

2018/12/03 04:32

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -79,3 +79,153 @@
79
79
  85
80
80
 
81
81
  ```
82
+
83
+
84
+
85
+ ---
86
+
87
+ 少しリファクタして、さらに高級にしてみた。
88
+
89
+ ```Python
90
+
91
+ from collections import Counter
92
+
93
+
94
+
95
+
96
+
97
+ data = []
98
+
99
+ with open('short.sequ.txt') as fin:
100
+
101
+ for line in map(str.rstrip, fin):
102
+
103
+ if not line:
104
+
105
+ continue
106
+
107
+
108
+
109
+ if line.startswith('>'):
110
+
111
+ data.append([line, ''])
112
+
113
+ else:
114
+
115
+ data[-1][-1] += line
116
+
117
+
118
+
119
+ for tag, line in data:
120
+
121
+ print(tag)
122
+
123
+
124
+
125
+ print(len(line))
126
+
127
+ for k, count in Counter(line).most_common():
128
+
129
+ print(k, count)
130
+
131
+
132
+
133
+ print()
134
+
135
+ ```
136
+
137
+
138
+
139
+ **実行結果** [Wandbox](https://wandbox.org/permlink/XbS7h2hBNMcXxc6L)
140
+
141
+ ```
142
+
143
+ >YP_009518834.1 putative uncharacterized protein YjiT [Escherichia coli str. K-12 substr. MG1655]
144
+
145
+ 505
146
+
147
+ L 50
148
+
149
+ S 46
150
+
151
+ V 37
152
+
153
+ R 36
154
+
155
+ E 35
156
+
157
+ G 33
158
+
159
+ A 31
160
+
161
+ T 29
162
+
163
+ I 27
164
+
165
+ D 25
166
+
167
+ P 24
168
+
169
+ Q 23
170
+
171
+ F 23
172
+
173
+ K 19
174
+
175
+ N 19
176
+
177
+ Y 14
178
+
179
+ C 12
180
+
181
+ W 10
182
+
183
+ H 8
184
+
185
+ M 4
186
+
187
+
188
+
189
+ >YP_009518833.1 uncharacterized protein YtiA [Escherichia coli str. K-12 substr. MG1655]
190
+
191
+ 85
192
+
193
+ G 9
194
+
195
+ L 7
196
+
197
+ I 7
198
+
199
+ V 6
200
+
201
+ R 6
202
+
203
+ T 5
204
+
205
+ Q 5
206
+
207
+ P 5
208
+
209
+ C 5
210
+
211
+ E 4
212
+
213
+ F 4
214
+
215
+ S 4
216
+
217
+ A 4
218
+
219
+ D 3
220
+
221
+ W 3
222
+
223
+ M 2
224
+
225
+ K 2
226
+
227
+ H 2
228
+
229
+ Y 2
230
+
231
+ ```

2

修正

2018/12/03 04:29

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
  ---
34
34
 
35
- メモリ潤沢に使って良いのなら、もっとシンプルに書けす。
35
+ 要件考えれば、もっとシンプルに書けそうです。
36
36
 
37
37
  ```Python
38
38
 

1

追記

2018/12/03 04:13

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -25,3 +25,57 @@
25
25
 
26
26
 
27
27
  おそらく内包表記の意味を勘違いして覚えています。
28
+
29
+
30
+
31
+ 書いてみた
32
+
33
+ ---
34
+
35
+ メモリを潤沢に使って良いのなら、もっとシンプルに書けます。
36
+
37
+ ```Python
38
+
39
+ data = []
40
+
41
+ with open('short.sequ.txt') as fin:
42
+
43
+ for line in map(str.rstrip, fin):
44
+
45
+ if not line:
46
+
47
+ continue
48
+
49
+
50
+
51
+ if line.startswith('>'):
52
+
53
+ data.append([line, 0])
54
+
55
+ else:
56
+
57
+ data[-1][-1] += len(line)
58
+
59
+
60
+
61
+ for datum in data:
62
+
63
+ print(*datum, sep='\n')
64
+
65
+ ```
66
+
67
+
68
+
69
+ **実行結果** [Wandbox](https://wandbox.org/permlink/oDw1bHFZaPRhwlWo)
70
+
71
+ ```
72
+
73
+ >YP_009518834.1 putative uncharacterized protein YjiT [Escherichia coli str. K-12 substr. MG1655]
74
+
75
+ 505
76
+
77
+ >YP_009518833.1 uncharacterized protein YtiA [Escherichia coli str. K-12 substr. MG1655]
78
+
79
+ 85
80
+
81
+ ```