Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中使用的min和max函数的输出结果是相同的。为什么?_Python_List_Max_Min - Fatal编程技术网

python中使用的min和max函数的输出结果是相同的。为什么?

python中使用的min和max函数的输出结果是相同的。为什么?,python,list,max,min,Python,List,Max,Min,收入和费用的数据已经给出,我们必须使用这些数据来计算剩余的数量 这是一段代码,使用python中列表的基本概念处理给定组织的财务分析 你打印错了。 因此,您正在使用for循环进行迭代。在for循环之后,直接使用i,它将是最后一个索引 所以,你需要做的是,得到最佳月份为真的索引并打印该索引。因此,您需要执行以下操作 profit = [] tax_profit = [] p_margin = [] mean = 0 for i in range(0,len(revenue)): prof

收入和费用的数据已经给出,我们必须使用这些数据来计算剩余的数量

这是一段代码,使用python中列表的基本概念处理给定组织的财务分析

你打印错了。 因此,您正在使用for循环进行迭代。在for循环之后,直接使用i,它将是最后一个索引

所以,你需要做的是,得到最佳月份为真的索引并打印该索引。因此,您需要执行以下操作

profit = []
tax_profit = []
p_margin = []
mean = 0

for i in range(0,len(revenue)):
    profit.append(revenue[i] - expenses[i])
print ("The profit per month is",profit)
print("\n")   

for i in range(0,len(profit)):
    tax_profit.append(profit[i] * 70/100)
print("The profit after tax is",tax_profit)
print("\n")


for i in range(0,len(tax_profit)):
    p_margin.append((tax_profit[i]/revenue[i]))
print(p_margin)
p_margin = [round(i,2) for i in p_margin]
print(p_margin)
print("\n")

mean_pat = sum(profit) / len(profit)
print(mean_pat)


good_months = []
bad_months = []
for i in range (0, len(tax_profit)):
    good_months.append(tax_profit[i] > mean_pat)
print(good_months)

for i in range (0, len(tax_profit)):
    bad_months.append(tax_profit[i] < mean_pat)
print(bad_months)

best_month = []
for i in range (0, len(tax_profit)):
    best_month.append(tax_profit[i] == max(tax_profit))
print(best_month)
print (round(tax_profit[i],2))
print("\n")

worst_month = []
for i in range (0, len(tax_profit)):
    worst_month.append(tax_profit[i] == min(tax_profit))
print(worst_month) 
print(round(tax_profit[i],2))          

> the output for best month and worst month is coming out to be same.
> Although the month having the worst month is showing True but printing the equivalent value of the worst month incorrectly by giving the value of the best month.

您甚至不需要索引回数据,只需打印最小值和最大值:

best_month_val  = print(round(tax_profit[best_month.index(True)],2))  
worst_month_val  = print(round(tax_profit[worst_month.index(True)],2))  

还考虑将MI/MAX值存储在变量中以避免重新计算-不确定Python是否优化了这个Max/min的计算或在每次迭代时对其进行重新计算:

应该是

best_month = []
for i in range (0, len(tax_profit)):
    best_month.append(tax_profit[i] == max(tax_profit))
print(best_month)
也就是说,如果您使用列表理解,您可以将代码缩短一点

其他错误:

你应该在好月份或坏月份中包括平均月份,目前它们都不在列表中 您的mean_-pat计算不使用税利润,而是使用税前:利润-因此之后的所有其他值都是倾斜的-请参见注释:
链接到-dokumentation-如果您在相同长度的列表上操作,并且必须对列表的值进行配对,则非常方便-对于长度不相等的列表,您可以使用它,以便为较短列表的值提供默认值。

欢迎使用Stackoverflow。您需要投入一些精力,并生成一个最小的可验证的完整示例来突出您的问题。不要只是做一个核心转储。看看为什么每次循环迭代都要计算max和min?税收利润不变,因此其最大值和最小值也不变。在循环之外计算它们,这样效率更高。此外,如果直接在循环项上循环,而不是使用索引循环,那么代码将更易于阅读。要并行循环2个或多个列表,请使用zip。若要在列表上循环并同时获取索引和列表项,请使用enumerate。Python的优化非常简单,因此它通常无法优化循环中的常量计算,例如那些max和min调用。@PM2Ring感谢您的确认,我只是有一个预感;
print(min(tax_profit))
print(max(tax_profit))
best_month = []
for i in range (0, len(tax_profit)):
    best_month.append(tax_profit[i] == max(tax_profit))
print(best_month)
 best_month = []
 maxValue = max(tax_profit)
 for i in range (0, len(tax_profit)):
     best_month.append(tax_profit[i] == maxValue)
 print(best_month)
revenue = [14574.49, 7606.46, 8611.41, 9175.41, 8058.65, 8105.44, 11496.28, 9766.09, 
           10305.32, 14379.96, 10713.97, 15433.50]
expenses = [12051.82, 5695.07, 12319.20, 12089.72, 8658.57, 840.20, 3285.73, 5821.12, 
            6976.93, 16618.61, 10054.37, 3803.96]

profit = [r-e for r,e in zip(revenue,expenses)]
tax_profit = [p * 70/100 for p in profit]
p_margin = [round(tp/rev,2) for tp,rev in zip(tax_profit,revenue)]
mean = 0  # never used?

print ("The profit per month is",profit,"\n")   

print("The profit after tax is",tax_profit,"\n")

print("The margin is",p_margin,"\n")

mean_pat = sum(profit) / len(profit)     # this is NOT after tax, its before
print("Mean Profit after tax:",mean_pat) # so the variable name is false? you reuse
print("\n")                              # it further down, you should review that   

# one of both should entail the month with mean_pat (if present)
good_months = [ tp >= mean_pat for tp in tax_profit]
bad_months = [tp < mean_pat for tp in tax_profit]

print("Good:",good_months,"\n")
print("Bad: ",bad_months,"\n")

maxTP = max(tax_profit)
best_month = [1 if p == maxTP else 0 for p in tax_profit]
print("Best Months:",best_month)
print ("Best result:",round(maxTP,2),"\n")

minTP = min(tax_profit)
worst_month = [1 if p == minTP else 0 for p in tax_profit]
print("Worst months:", worst_month) 
print("Worst result:",round(minTP,2),"\n")
The profit per month is [2522.67, 1911.3900000000003, -3707.790000000001, -2914.3099999999995, -599.9200000000001, 7265.24, 8210.550000000001, 3944.9700000000003, 3328.3899999999994, -2238.6500000000015, 659.5999999999985, 11629.54]

The profit after tax is [1765.869, 1337.9730000000002, -2595.4530000000004, -2040.0169999999996, -419.9440000000001, 5085.668, 5747.385000000001, 2761.4790000000003, 2329.8729999999996, -1567.0550000000012, 461.719999999999, 8140.6780000000
01]

The margin is [0.12, 0.18, -0.3, -0.22, -0.05, 0.63, 0.5, 0.28, 0.23, -0.11, 0.04, 0.53]

Mean Profit after tax: 2500.9733333333334


Good: [False, False, False, False, False, True, True, True, False, False, False, True]

Bad:  [True, True, True, True, True, False, False, False, True, True, True, False]

Best Months: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
Best result: 8140.68

Worst months: [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Worst result: -2595.45