teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

6

追記

2018/08/28 13:45

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -57,4 +57,14 @@
57
57
 
58
58
  > エラーが出ます(next()を試した場合も出てきました)
59
59
 
60
- next自身はメソッドではなくグローバル関数であることに留意が必要です。
60
+ next自身はメソッドではなくグローバル関数であることに留意が必要です。
61
+
62
+ 追記
63
+ ---
64
+ ご提示のコードに限って言えば、もっと簡単に書けると思います。
65
+ ```Python
66
+ with open('result.out', mode='r+') as fin:
67
+ Z = np.array(
68
+ [int(line.split()[-1]) for line in fin], dtype=float
69
+ ).reshape(10, 10)
70
+ ```

5

追記

2018/08/28 13:45

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -45,4 +45,16 @@
45
45
  def gen_next_token(fin):
46
46
  for line in map(str.strip, fin):
47
47
  yield from line.split()
48
- ```
48
+ ```
49
+
50
+ 質問追記を受けて
51
+ ---
52
+ > ```
53
+ AttributeError: '_io.TextIOWrapper' object has no attribute 'readLine'
54
+ > ```
55
+
56
+ タイポです。`readLine` → `readline`
57
+
58
+ > エラーが出ます(next()を試した場合も出てきました)
59
+
60
+ next自身はメソッドではなくグローバル関数であることに留意が必要です。

4

追記

2018/08/28 13:29

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -33,9 +33,16 @@
33
33
 
34
34
  こんなのを用意しても良いかもしれませんね。
35
35
  ```Python
36
+ from itertools import chain
37
+
36
38
  def gen_next_token(fin):
39
+ return chain.from_iterable(
40
+ line.split() for line in map(str.strip, fin)
41
+ )
42
+ ```
43
+
44
+ ```Python
45
+ def gen_next_token(fin):
37
46
  for line in map(str.strip, fin):
38
47
  yield from line.split()
39
- ```
48
+ ```
40
-
41
- [Wandbox](https://wandbox.org/permlink/kxZ4PoPvomQeU4tS)

3

修正

2018/08/28 13:23

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -33,12 +33,9 @@
33
33
 
34
34
  こんなのを用意しても良いかもしれませんね。
35
35
  ```Python
36
- def make_next_token(fin):
36
+ def gen_next_token(fin):
37
- def next_token():
38
- for line in map(str.strip, fin):
37
+ for line in map(str.strip, fin):
39
- yield from line.split()
38
+ yield from line.split()
40
-
41
- return next_token
42
39
  ```
43
40
 
44
- [Wandbox](https://wandbox.org/permlink/zpaGb8n5dJuUBFry)
41
+ [Wandbox](https://wandbox.org/permlink/kxZ4PoPvomQeU4tS)

2

追記

2018/08/28 13:22

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -27,4 +27,18 @@
27
27
 
28
28
  [Wandbox](https://wandbox.org/permlink/umgpQedoKIwu39su)
29
29
 
30
- **追記:** ファイルの場合は[readline](https://docs.python.jp/3/library/io.html#io.IOBase.readline)の方が自然ですね。
30
+ **追記:** ファイルの場合は[readline](https://docs.python.jp/3/library/io.html#io.IOBase.readline)の方が自然ですね。
31
+
32
+ > もっと言えば、javaやc++のような空白区切りで”1つずつ”読み込める関数があればうれしいです・・・
33
+
34
+ こんなのを用意しても良いかもしれませんね。
35
+ ```Python
36
+ def make_next_token(fin):
37
+ def next_token():
38
+ for line in map(str.strip, fin):
39
+ yield from line.split()
40
+
41
+ return next_token
42
+ ```
43
+
44
+ [Wandbox](https://wandbox.org/permlink/zpaGb8n5dJuUBFry)

1

追記

2018/08/28 13:15

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -25,4 +25,6 @@
25
25
  pass
26
26
  ```
27
27
 
28
- [Wandbox](https://wandbox.org/permlink/umgpQedoKIwu39su)
28
+ [Wandbox](https://wandbox.org/permlink/umgpQedoKIwu39su)
29
+
30
+ **追記:** ファイルの場合は[readline](https://docs.python.jp/3/library/io.html#io.IOBase.readline)の方が自然ですね。