Reputation: 69
the last part of my program is not showing up, can someone help me?
Given a list of numbers, normalize the values in the list to be in the range [0,1]. First find the minimum and the maximum values in the list, min_val and max_val. Change every element, x, of the list to a new value, y, s.t. y = (x-min_val)/(max_val-min_val). Print the modified list. For example list: [2,1,3,4] becomes: [0.3333333333333333, 0.0, 0.6666666666666666, 1.0] You can hardcode a sample list in your program, but the solution should not depend on it. That is, if we only modify that list and rerun your program, it should produce the correct result based on the new list.
mylist=[3,1,5,9,2]
n=len(mylist)
newlist=[]
print("orginal list is:",mylist)
min_val=min(mylist)
print("minimum value is:",min_val)
max_val=max(mylist)
print("maximum value is:",max_val)
for x in range(1+n+1,1):
newlist=((x-min_val)/(max_val-min_val))
print(newlist)
Upvotes: 0
Views: 575
Reputation: 682
First of all, you are re-assigning newlist every step of the for.
for x in range(1+n+1,1):
newlist.append((x-min_val)/(max_val-min_val))
print(newlist)
Then, you most likely need to fix your 'range' function [If using python2]; And finally you probably need to make those int into floats to avoid integer division [If using python2]
for x in mylist:
newlist.append(float(x-min_val)/(max_val-min_val))
print(newlist)
Upvotes: 1
Reputation: 65791
In the last part, you don't do anything with the elements of mylist
at all.
You need something like
newlist = [(x - min_val)/(max_val - min_val) for x in mylist]
Using an explicit for
loop, like you are trying to do:
newlist = []
for x in mylist: # sic!
newlist.append((x - min_val) / (max_val - min_val))
Upvotes: 4