0
入力ファイル内の値のすべてのインデックスを検索しようとしています。プログラムは、検索する番号を第1のコマンドライン引数として、入力ファイルの名前を第2の引数として受け付けます。入力ファイルから値のすべてのインデックスを出力する方法
import sys
value = sys.argv[1]
file_name = sys.argv[2]
file = open(file_name, 'r')
print('The value of file \"{}\" to be searched:\n{}'.format(file_name, value))
for line in file.readlines():
curr_arr = []
for i in line.split(','):
curr_arr +=[int(i)]
def find_values(curr_arr, val):
found_indexes = []
sum = 0
for n in range(len(curr_arr)):
sum += curr_arr[n]
if curr_arr[n] == val:
found_indexes += [n]
sum = sum + 1
return found_indexes
print 'The index(s) of the occurrence of {} in {}: {}'.format(value, curr_arr, find_values(curr_arr, value))
そして、これは私が得るものです:私は、値は以下のコードから見つかった時の指数出力にトラブルを抱えてい
a1.py 7 text.csv
The value of file "text.csv" to be searched:
7
The index(s) of the occurrence of 7 in [2, 3, 7, 9, 7, 3, 2]:
Iは、[2を得るためになっています、 4]を返しますが、何も返しません。コードを教えてもらえますか?ありがとうございます
読み込んだ行(curr_arr = [] ')ごとに' curr_arr'をリセットする方法に注意してください。 – math2001