回答編集履歴

6

追記

2018/08/28 13:45

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -117,3 +117,23 @@
117
117
 
118
118
 
119
119
  next自身はメソッドではなくグローバル関数であることに留意が必要です。
120
+
121
+
122
+
123
+ 追記
124
+
125
+ ---
126
+
127
+ ご提示のコードに限って言えば、もっと簡単に書けると思います。
128
+
129
+ ```Python
130
+
131
+ with open('result.out', mode='r+') as fin:
132
+
133
+ Z = np.array(
134
+
135
+ [int(line.split()[-1]) for line in fin], dtype=float
136
+
137
+ ).reshape(10, 10)
138
+
139
+ ```

5

追記

2018/08/28 13:45

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -93,3 +93,27 @@
93
93
  yield from line.split()
94
94
 
95
95
  ```
96
+
97
+
98
+
99
+ 質問追記を受けて
100
+
101
+ ---
102
+
103
+ > ```
104
+
105
+ AttributeError: '_io.TextIOWrapper' object has no attribute 'readLine'
106
+
107
+ > ```
108
+
109
+
110
+
111
+ タイポです。`readLine` → `readline`
112
+
113
+
114
+
115
+ > エラーが出ます(next()を試した場合も出てきました)
116
+
117
+
118
+
119
+ next自身はメソッドではなくグローバル関数であることに留意が必要です。

4

追記

2018/08/28 13:29

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -68,6 +68,24 @@
68
68
 
69
69
  ```Python
70
70
 
71
+ from itertools import chain
72
+
73
+
74
+
75
+ def gen_next_token(fin):
76
+
77
+ return chain.from_iterable(
78
+
79
+ line.split() for line in map(str.strip, fin)
80
+
81
+ )
82
+
83
+ ```
84
+
85
+
86
+
87
+ ```Python
88
+
71
89
  def gen_next_token(fin):
72
90
 
73
91
  for line in map(str.strip, fin):
@@ -75,7 +93,3 @@
75
93
  yield from line.split()
76
94
 
77
95
  ```
78
-
79
-
80
-
81
- [Wandbox](https://wandbox.org/permlink/kxZ4PoPvomQeU4tS)

3

修正

2018/08/28 13:23

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -68,20 +68,14 @@
68
68
 
69
69
  ```Python
70
70
 
71
- def make_next_token(fin):
71
+ def gen_next_token(fin):
72
72
 
73
- def next_token():
73
+ for line in map(str.strip, fin):
74
74
 
75
- for line in map(str.strip, fin):
76
-
77
- yield from line.split()
75
+ yield from line.split()
78
-
79
-
80
-
81
- return next_token
82
76
 
83
77
  ```
84
78
 
85
79
 
86
80
 
87
- [Wandbox](https://wandbox.org/permlink/zpaGb8n5dJuUBFry)
81
+ [Wandbox](https://wandbox.org/permlink/kxZ4PoPvomQeU4tS)

2

追記

2018/08/28 13:22

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -57,3 +57,31 @@
57
57
 
58
58
 
59
59
  **追記:** ファイルの場合は[readline](https://docs.python.jp/3/library/io.html#io.IOBase.readline)の方が自然ですね。
60
+
61
+
62
+
63
+ > もっと言えば、javaやc++のような空白区切りで”1つずつ”読み込める関数があればうれしいです・・・
64
+
65
+
66
+
67
+ こんなのを用意しても良いかもしれませんね。
68
+
69
+ ```Python
70
+
71
+ def make_next_token(fin):
72
+
73
+ def next_token():
74
+
75
+ for line in map(str.strip, fin):
76
+
77
+ yield from line.split()
78
+
79
+
80
+
81
+ return next_token
82
+
83
+ ```
84
+
85
+
86
+
87
+ [Wandbox](https://wandbox.org/permlink/zpaGb8n5dJuUBFry)

1

追記

2018/08/28 13:15

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -53,3 +53,7 @@
53
53
 
54
54
 
55
55
  [Wandbox](https://wandbox.org/permlink/umgpQedoKIwu39su)
56
+
57
+
58
+
59
+ **追記:** ファイルの場合は[readline](https://docs.python.jp/3/library/io.html#io.IOBase.readline)の方が自然ですね。