-1

I have a datafile, from which I only require the maximum value contained within the 3rd column. I wrote the following code:

def testing():
file_reading = open("source_file.txt", "r", encoding="utf-8") 
file_steps_yearly_sum = 0
file_steps_daily_list = []
while (True):
    row = file_reading.readline() 
    if len(row) == 0: 
        break
    else:
        steps = row.split(';')[2]
        file_steps_yearly_sum = int(file_steps_yearly_sum) + int(steps)
        #file_steps_list.append(row.split(';')[2])
        file_steps_daily_list.append(steps)
print(max(file_steps_daily_list))
print(file_steps_yearly_sum)
file_reading.close()
return file_steps_daily_list, file_steps_yearly_sum

The calculated sum is correct (checked it with excel), but the max method gives me 9928, which is clearly wrong. Just looking at the input file I can see values well over 10 000 present. I don't get it, should be a one-liner.

Just to be sure, did the same with a loop:

def max_num_in_list(file_steps_daily_list):
max = file_steps_daily_list[ 0 ]
for steps in file_steps_daily_list:
    if steps > max:
        max = steps
return max

Again, 9928. The list I extracted from the 3rd column of the source file was:

['3766', '12049', '14624', '12244', '12353', '12883', '11079', '10382', '11491', '14863', '8607', '15768', '9308', '10369', '11240', '12316', '14224', '10806', '9511', '7680', '12966', '11112', '14996', '15939', '11097', '5904', '15145', '9319', '15927', '11093', '8978', '9777', '10945', '9928', '12438', '10862', '12461', '11435', '15788', '10483', '5110', '15148', '12534', '12806', '14360', '10890', '11102', '6314', '10931', '12354', '6986', '14972', '13312', '10307', '9032', '10941', '8279', '10040', '14651', '8613', '7798', '13348', '10173', '15510', '15158', '13350', '10725', '8819', '17705', '12188', '10588', '9329', '9855', '8263', '8568', '7943', '20916', '15909', '23344', '14633', '31589', '17501', '15623', '27245', '14883', '13990', '11854', '12016', '4131', '5286', '6542', '6165', '9261', '11257', '11418', '5848', '6240', '4718', '10072', '7737', '6313', '13311', '14479', '6472', '6411', '7662', '8918', '13317', '17446', '5920', '7435', '9381', '12857', '7828', '8645', '12074', '12183', '8771', '4522', '5575', '8379', '8759', '8803', '7046', '12604', '10126', '11097', '8137', '6297', '10037', '5261', '5498', '10552', '4577', '8059', '5404', '4995', '8932', '6457', '10056', '9578', '11464', '11847', '5212', '13996', '11114', '8130', '10293', '9456', '12363', '8990', '11352', '5656', '15293', '5777', '8666', '8810', '8257', '6530', '10545', '8240', '10257', '8968', '10885', '14032', '14824', '14065', '9459', '11654', '13685', '20181', '11789', '15220', '17844', '17542', '9152', '11382', '9120', '10311', '14250', '19197', '22475', '12806', '13575', '7773', '4581', '14411', '10262', '25511', '17731', '10729', '16751', '13167', '14036', '12650', '11697', '12388', '14691', '18434', '5770', '9392', '9802', '10770', '10666', '10270', '12637', '16612', '10226', '9296', '6447', '10558', '8255', '11657', '10749', '10691', '10692', '10202', '10129', '6985', '8428', '13951', '10186', '7663', '10325', '8022', '6410', '13082', '8145', '8342', '6718', '7665', '4771', '10337', '11529', '10757', '10578', '8330', '10803', '10465', '4351', '10575', '18682', '10668', '7541', '10318', '11217', '6072', '14779', '14593', '10970', '9538', '11130', '16437', '4732', '10523', '9428', '4023', '8045', '9397', '12765', '8888', '11324', '10952', '8924', '10643', '11467', '15881', '11923', '13584', '13399', '11940', '10092', '7739', '9582', '11673', '8454', '11401', '10040', '11706', '10589', '7475', '9097', '14294', '14672', '10227', '7452', '11680', '14223', '15035', '11698', '15239', '8878', '9530', '11068', '6766', '10737', '10805', '13559', '9126', '10331', '8525', '4224', '5898', '7549', '14848', '11776', '13216', '8420', '6104', '6193', '7448', '8403', '7286', '11457', '11537', '4463', '6693', '3723', '12183', '10768', '18342', '13224', '10874', '7713', '12385', '14665', '10184', '10272', '11718', '12757', '11296', '18269', '11295', '10987', '11690', '5036', '7402', '8013', '12001', '8734', '12941', '10298', '11536', '10947', '14931', '18185', '9139']

Obviously, 9928 is not the maximum value. What is going on? Why max() won't give me the maximum value?

4
  • 3
    Note that file_steps_daily_list is a list of strings, not a list of ints. Commented Jun 23, 2021 at 13:44
  • 1
    change the last line in the else block to file_steps_daily_list.append(int(steps)). Commented Jun 23, 2021 at 13:45
  • @Carcigenicate, Oww. Yes, I see now Commented Jun 23, 2021 at 13:46
  • This question is similar to: Find the greatest (largest, maximum) number in a list of numbers. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Feb 21 at 19:33

3 Answers 3

1

try this:

a = ['3766', '12049', '14624', '12244', '12353', '12883', '11079', '10382', '11491', '14863', '8607', '15768', '9308', '10369', '11240', '12316', '14224', '10806', '9511', '7680', '12966', '11112', '14996', '15939', '11097', '5904', '15145', '9319', '15927', '11093', '8978', '9777', '10945', '9928', '12438', '10862', '12461', '11435', '15788', '10483', '5110', '15148', '12534', '12806', '14360', '10890', '11102', '6314', '10931', '12354', '6986', '14972', '13312', '10307', '9032', '10941', '8279', '10040', '14651', '8613', '7798', '13348', '10173', '15510', '15158', '13350', '10725', '8819', '17705', '12188', '10588', '9329', '9855', '8263', '8568', '7943', '20916', '15909', '23344', '14633', '31589', '17501', '15623', '27245', '14883', '13990', '11854', '12016', '4131', '5286', '6542', '6165', '9261', '11257', '11418', '5848', '6240', '4718', '10072', '7737', '6313', '13311', '14479', '6472', '6411', '7662', '8918', '13317', '17446', '5920', '7435', '9381', '12857', '7828', '8645', '12074', '12183', '8771', '4522', '5575', '8379', '8759', '8803', '7046', '12604', '10126', '11097', '8137', '6297', '10037', '5261', '5498', '10552', '4577', '8059', '5404', '4995', '8932', '6457', '10056', '9578', '11464', '11847', '5212', '13996', '11114', '8130', '10293', '9456', '12363', '8990', '11352', '5656', '15293', '5777', '8666', '8810', '8257', '6530', '10545', '8240', '10257', '8968', '10885', '14032', '14824', '14065', '9459', '11654', '13685', '20181', '11789', '15220', '17844', '17542', '9152', '11382', '9120', '10311', '14250', '19197', '22475', '12806', '13575', '7773', '4581', '14411', '10262', '25511', '17731', '10729', '16751', '13167', '14036', '12650', '11697', '12388', '14691', '18434', '5770', '9392', '9802', '10770', '10666', '10270', '12637', '16612', '10226', '9296', '6447', '10558', '8255', '11657', '10749', '10691', '10692', '10202', '10129', '6985', '8428', '13951', '10186', '7663', '10325', '8022', '6410', '13082', '8145', '8342', '6718', '7665', '4771', '10337', '11529', '10757', '10578', '8330', '10803', '10465', '4351', '10575', '18682', '10668', '7541', '10318', '11217', '6072', '14779', '14593', '10970', '9538', '11130', '16437', '4732', '10523', '9428', '4023', '8045', '9397', '12765', '8888', '11324', '10952', '8924', '10643', '11467', '15881', '11923', '13584', '13399', '11940', '10092', '7739', '9582', '11673', '8454', '11401', '10040', '11706', '10589', '7475', '9097', '14294', '14672', '10227', '7452', '11680', '14223', '15035', '11698', '15239', '8878', '9530', '11068', '6766', '10737', '10805', '13559', '9126', '10331', '8525', '4224', '5898', '7549', '14848', '11776', '13216', '8420', '6104', '6193', '7448', '8403', '7286', '11457', '11537', '4463', '6693', '3723', '12183', '10768', '18342', '13224', '10874', '7713', '12385', '14665', '10184', '10272', '11718', '12757', '11296', '18269', '11295', '10987', '11690', '5036', '7402', '8013', '12001', '8734', '12941', '10298', '11536', '10947', '14931', '18185', '9139']

int_list = list(map(int,a))

print(max(int_list)) #31589
Sign up to request clarification or add additional context in comments.

Comments

1

This looks like a list of strings, and the max function compares the char value of each character, not the integer value of the number.

Cast list elements to int first, like you did for the sum.

print(max([int(i) for i in file_steps_daily_list]))

Comments

1

First make sure steps is an int then use it across board

...
else:
    steps = int(row.split(';')[2])
    file_steps_yearly_sum = int(file_steps_yearly_sum) + steps
    #file_steps_list.append(row.split(';')[2])
    file_steps_daily_list.append(steps)

and then you can print

print(max(file_steps_daily_list))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.