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

回答編集履歴

2

forのカウンター不要でした。失礼しました。

2016/06/01 09:49

投稿

toshi_yama
toshi_yama

スコア133

answer CHANGED
@@ -8,7 +8,7 @@
8
8
  file_contents = input_file.read()
9
9
  list_of_numbers = file_contents.split()
10
10
  input_file.close()
11
- for i,num in enumerate(list_of_numbers):
11
+ for num in list_of_numbers:
12
12
  val1,val2 = num.split(',')
13
13
  numbers.append((int(val1),int(val2)))
14
14
  return numbers

1

回答の変更

2016/06/01 09:49

投稿

toshi_yama
toshi_yama

スコア133

answer CHANGED
@@ -1,20 +1,20 @@
1
- numbersがリストになってますね。
1
+ タプルのリストを作りたいということですね。
2
+ 以下でどうでしょう。
2
3
 
3
4
  ```python
4
5
  def read_numbers(filename):
5
- numbers = ()
6
+ numbers=[]
6
7
  input_file = open(filename, "r")
7
8
  file_contents = input_file.read()
8
9
  list_of_numbers = file_contents.split()
9
10
  input_file.close()
10
- for i in list_of_numbers:
11
+ for i,num in enumerate(list_of_numbers):
12
+ val1,val2 = num.split(',')
11
- numbers += i,
13
+ numbers.append((int(val1),int(val2)))
12
14
  return numbers
13
15
 
14
16
  print(read_numbers('output10.txt'))
15
17
  ```
16
18
 
17
19
  出力
18
- ('68,125', '113,69', '65,86', '108,149', '152,53', '78,90', '54,160', '20,137'・・・)
20
+ [(68,125), (113,69), (65,86), (108,149), (152,53), (78,90), (54,160), (20,137)・・・]
19
-
20
- こういった形で大丈夫ですか?