Предполагая, что у меня есть ответы студентов в диапазоне 0-5 в списке, и цель состоит в том, чтобы преобразовать диапазон в 0-10. Я написал небольшую функцию Python (на основе предыдущего поста: Преобразование диапазона чисел в другой диапазон, поддерживая отношение ), чтобы выполнить это преобразование, чтобы перейти к следующему шагу моего конвейера анализа. Основная проблема здесь заключается в том, что выходной список не содержит нулей и хранит только все, что выше нуля. (Он не может передавать ноль от входа к выходу).
Вот список, который я использовал как ВХОД:
значение
1
0
0
0
0
0
0
3
и теперь код с некоторыми комментариями:
# 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)
А вот и вывод
[1,0, 5,5]
где нули исходного ввода:
[1, 0, 0, 0, 0, 0, 0, 3]
Я хотел бы вывод, например:
[1,0, 0,, 0, 0, 0, 0, 0 5.5]