0

Assuming that I have students’ answers in a range 0-5 in a list and the target is to convert the range to 0-10. I wrote a small python function (based on a previous post: Convert a number range to another range, maintaining ratio) to do this conversion in order to proceed to the next step of my analysis pipeline. The major problem here is that the output list doesn’t include zeros and only stores everything above zero. (It cannot pass zero from input to output).

Here is the list that I have used as INPUT :

value 1 0 0 0 0 0 0 3

and now the code with some comments :

# the appropriate libs
import pandas as pd
import csv

#read as dataframe
current_data = pd.read_csv('current.csv')

# convert it to a list
convert_to_list = current_data['value'].values.tolist()

# test the input
print(convert_to_list)

# make a function that accepts the list as an argument
def convert1to10(adf):
# initiate vars and set the ranges
    OldMax = 5
    OldMin = 1
    NewMax = 10
    NewMin = 1
    NewValue = 0
# define the range
    OldRange = (OldMax - OldMin)
# make new array to store the output later
    new_array = []
# make a loop through the length of the list
    for position in range(len(adf)):
# just set the newvalue as newmin as zero in case the input range (OldRange) is 0 
            if (OldRange == 0):
                NewValue = NewMin
            else:
# set the new range as new max - newmin
                NewRange = (NewMax - NewMin)
# the new value is the current possition in the loop - the old min multiplied by the new range and divided by the old range + NewMin  
                NewValue = (((adf[position] - OldMin) * NewRange) / OldRange) + NewMin
# append the new value in every loop in the new_array
                new_array.append(NewValue)
# return the list   
    return new_array


# call the funtion with the list as argument
calldef_test = convert1to10(convert_to_list)
# print the result
print(calldef_test)

And here is the output

[1.0, 5.5]

where are the zeros of the original input :

[1, 0, 0, 0, 0, 0, 0, 3]

I would like an output like :

[1.0, 0, ,0 ,0 ,0 ,0 ,0 5.5]

Gizmapps
  • 59
  • 1
  • 10

1 Answers1

1

In if (OldRange == 0): condition you are not adding the zero in the array which you might need to add see the below code:

if (OldRange == 0):
    NewValue = NewMin
    new_array.append(OldRange)
else:
# set the new range as new max - newmin
    NewRange = (NewMax - NewMin)
# the new value is the current possition in the loop - the old min multiplied by the new range and divided by the old range + NewMin
    NewValue = (((adf[position] - OldMin) * NewRange) / OldRange) + NewMin
# append the new value in every loop in the new_array
    new_array.append(NewValue)
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
  • actually, I have managed to overcome this based on your idea elif (adf[position] == 0): new_array.append(adf[position]) – Gizmapps Apr 03 '19 at 18:25
  • elif (adf[position] == 0): new_array.append(adf[position]) This did the trick THANKS!!! – Gizmapps Apr 03 '19 at 18:27