Hi everyone,
I'm trying to write a program to find all the indices of a value being searched from an input file. The program will accept the number to be searched for as 1st command line argument, and the name of the input file as the second argument. I'm having trouble to output the indices at which the value was found from the code below:
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]
return found_indexes
print 'The index(s) of the occurrence of {} in {}: {}'.format(value, curr_arr, find_values(curr_arr, value))
And this is what I get:
>>>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 supposed to get [2, 4] but it returns none. Could anyone help me with the code? Thank you
I'm trying to write a program to find all the indices of a value being searched from an input file. The program will accept the number to be searched for as 1st command line argument, and the name of the input file as the second argument. I'm having trouble to output the indices at which the value was found from the code below:
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]
return found_indexes
print 'The index(s) of the occurrence of {} in {}: {}'.format(value, curr_arr, find_values(curr_arr, value))
And this is what I get:
>>>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 supposed to get [2, 4] but it returns none. Could anyone help me with the code? Thank you