Hi everyone,

I'm new to posting, but I've been coming here to search for help for some time now. I'm working on some homework and have come to an impasse. I'm not here for straight-up solutions, just a little help, as I've already got the program 99% complete. In the program I'm trying to scale a list of numbers, but my method for identifying the maximum element is obviously flawed. Could someone help me spot why this program does not think that 100 is greater than 95?

f=open('list.txt','r')

numlist=f.readlines()

len = len(numlist)

max = (numlist[0])
min = (numlist[0])

print "original list: "
for n in range(1,len):
        print numlist[n]
        if numlist[n]>max:
                max=(numlist[n])
        if numlist[n]<min:
                min=(numlist[n])

newlist = []

for n in range(0,len):
        x = float((int(numlist[n]) - float(min))/(int(max)-float(min)))
        newlist.append(x)
print "min: "
print min
print "max: "
print max
print "scaled list: "
print newlist

The "list.txt" file is just:

80
0
60
25
50
70
75
100
90
95

Thanks in advance!

Recommended Answers

All 2 Replies

The numbers come from the file as strings, not integers. Strings sort left to right so
100 --> 1 & (00) is less than
95 --> 9 & (5)
Hopefully this bit of code will help

test_int_list = [
80,
0,
60,
25,
50,
70,
75,
100,
90,
95 ]

test_str_list = [str(num) for num in test_int_list]
test_str_list.sort()
print "As strings", test_str_list
test_int_list.sort()
print "As integers", test_int_list

Thanks! I was so confused, and now it seems so obvious!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.