История изменений
Исправление paddlewan, (текущая версия) :
Я улучшил вашу программу.
Теперь вы хотя бы можете увидеть, что в ней на самом деле происходит (см. код ниже) :)
А вообще жесть :)
sing.index(max(sing)) - вернет первое вхождение максимального значения в list(). А что если оно не одно? Тогда почему первый индекс берем? Короч, это просто праздник какой-то в этом и других отношениях:)
import time
## import numpy as np was never used
CATEGORIES = ["0","1","2","3","4","5","6","7","8","9",
"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","R","S","T","U",
"V","W","X","Y","Z"]
DATA = [[0.002,0.006,0.004,0.045,0.002,0.017,0.006,0.077,0.001,0.035,0.042,0.005,0.004,0.039,0.001,0.002,0.001,0.008,0.058,0.352,0.002,0.007,0.017,0.004,0.007,0.007,0.007,0.004,0.005,0.009,0.089,0.036,0.053,0.041,0.004],[0.003,0.007,0.005,0.075,0.001,0.020,0.006,0.044,0.002,0.035,0.026,0.004,0.004,0.033,0.001,0.001,0.003,0.008,0.049,0.360,0.002,0.007,0.021,0.005,0.009,0.003,0.008,0.007,0.003,0.014,0.092,0.048,0.058,0.031,0.004],[0.002,0.000,0.025,0.012,0.006,0.002,0.001,0.627,0.006,0.021,0.022,0.008,0.004,0.006,0.004,0.033,0.000,0.006,0.011,0.009,0.002,0.002,0.009,0.000,0.002,0.040,0.007,0.005,0.015,0.000,0.035,0.001,0.008,0.015,0.053],[0.056,0.008,0.023,0.038,0.015,0.007,0.050,0.006,0.412,0.004,0.005,0.027,0.011,0.005,0.021,0.007,0.073,0.024,0.012,0.005,0.013,0.005,0.027,0.003,0.015,0.001,0.005,0.074,0.002,0.022,0.005,0.011,0.002,0.001,0.006],[0.025,0.011,0.025,0.034,0.018,0.027,0.090,0.008,0.258,0.006,0.007,0.026,0.016,0.008,0.026,0.011,0.079,0.030,0.026,0.008,0.018,0.011,0.033,0.003,0.016,0.001,0.003,0.106,0.004,0.021,0.012,0.013,0.003,0.005,0.014],[0.048,0.027,0.019,0.002,0.028,0.002,0.008,0.017,0.041,0.014,0.012,0.022,0.031,0.005,0.045,0.100,0.004,0.031,0.033,0.002,0.029,0.006,0.021,0.032,0.008,0.038,0.317,0.007,0.017,0.004,0.018,0.005,0.003,0.004,0.002],[0.013,0.002,0.002,0.000,0.164,0.001,0.060,0.004,0.006,0.002,0.018,0.003,0.035,0.002,0.008,0.008,0.001,0.008,0.028,0.005,0.383,0.013,0.063,0.010,0.004,0.002,0.014,0.016,0.002,0.005,0.048,0.011,0.028,0.017,0.012]]
'''
data here was never used
'''
def original():
start = time.time()
for lst in DATA:
print(CATEGORIES[lst.index(max(lst))])
'''
def list_mean(nums): ## the function has never been called
sumof = 0
num_of = len(nums)
mean = 0
for i in nums:
sumof += i
mean = sumof / num_of
return float(mean)
'''
def list_mean(nums): ## looks a bit better to me this way
return sum(nums) / len(nums)
end = time.time()
print(f"Runtime of original program is {end - start}")
def readable():
start = time.time()
for lst in DATA:
index, biggest = 0, -1 ## I assume we only have positives in the list
for i in range(len(lst)): ## might be quicker than calling max() on enumerate() + lambda. Whaddayathink, Aswed? :)
if lst[i] > biggest:
index, biggest = i, lst[i]
print(CATEGORIES[index])
end = time.time()
print(f"Runtime of new program is: {end - start} which should be exactly the same\nbut who knows... :)")
if __name__ == "__main__":
original()
readable()
Исходная версия paddlewan, :
Я улучшил вашу программу.
Теперь вы хотя бы можете увидеть, что в ней на самом деле происходит (см. код ниже) :)
А вообще жесть :)
sing.index(max(sing)) - вернет первое вхождение максимального значения в list(). А что если оно не одно? Тогда почему первый индекс берем? Короч, это просто праздник какой-то в этом и других отношениях:)
import time
## import numpy as np was never used
CATEGORIES = ["0","1","2","3","4","5","6","7","8","9",
"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","R","S","T","U",
"V","W","X","Y","Z"]
DATA = [[0.002,0.006,0.004,0.045,0.002,0.017,0.006,0.077,0.001,0.035,0.042,0.005,0.004,0.039,0.001,0.002,0.001,0.008,0.058,0.352,0.002,0.007,0.017,0.004,0.007,0.007,0.007,0.004,0.005,0.009,0.089,0.036,0.053,0.041,0.004],[0.003,0.007,0.005,0.075,0.001,0.020,0.006,0.044,0.002,0.035,0.026,0.004,0.004,0.033,0.001,0.001,0.003,0.008,0.049,0.360,0.002,0.007,0.021,0.005,0.009,0.003,0.008,0.007,0.003,0.014,0.092,0.048,0.058,0.031,0.004],[0.002,0.000,0.025,0.012,0.006,0.002,0.001,0.627,0.006,0.021,0.022,0.008,0.004,0.006,0.004,0.033,0.000,0.006,0.011,0.009,0.002,0.002,0.009,0.000,0.002,0.040,0.007,0.005,0.015,0.000,0.035,0.001,0.008,0.015,0.053],[0.056,0.008,0.023,0.038,0.015,0.007,0.050,0.006,0.412,0.004,0.005,0.027,0.011,0.005,0.021,0.007,0.073,0.024,0.012,0.005,0.013,0.005,0.027,0.003,0.015,0.001,0.005,0.074,0.002,0.022,0.005,0.011,0.002,0.001,0.006],[0.025,0.011,0.025,0.034,0.018,0.027,0.090,0.008,0.258,0.006,0.007,0.026,0.016,0.008,0.026,0.011,0.079,0.030,0.026,0.008,0.018,0.011,0.033,0.003,0.016,0.001,0.003,0.106,0.004,0.021,0.012,0.013,0.003,0.005,0.014],[0.048,0.027,0.019,0.002,0.028,0.002,0.008,0.017,0.041,0.014,0.012,0.022,0.031,0.005,0.045,0.100,0.004,0.031,0.033,0.002,0.029,0.006,0.021,0.032,0.008,0.038,0.317,0.007,0.017,0.004,0.018,0.005,0.003,0.004,0.002],[0.013,0.002,0.002,0.000,0.164,0.001,0.060,0.004,0.006,0.002,0.018,0.003,0.035,0.002,0.008,0.008,0.001,0.008,0.028,0.005,0.383,0.013,0.063,0.010,0.004,0.002,0.014,0.016,0.002,0.005,0.048,0.011,0.028,0.017,0.012]]
'''
data here was never used
'''
def original():
start = time.time()
for lst in DATA:
print(CATEGORIES[lst.index(max(lst))])
'''
def list_mean(nums): ## the function has never been called
sumof = 0
num_of = len(nums)
mean = 0
for i in nums:
sumof += i
mean = sumof / num_of
return float(mean)
'''
def list_mean(nums): ## looks a bit better to me this way
return sum(nums) / len(nums)
end = time.time()
print(f"Runtime of original program is {end - start}")
def readable():
start = time.time()
for lst in DATA:
index, biggest = 0, -1 ## I assume we only have positives in the list
for i in range(len(lst)): ## might be quicker than enumerate() + lambda. Whaddayathink, Aswed? :)
if lst[i] > biggest:
index, biggest = i, lst[i]
print(CATEGORIES[index])
end = time.time()
print(f"Runtime of new program is: {end - start} which should be exactly the same\nbut who knows... :)")
if __name__ == "__main__":
original()
readable()