简体   繁体   English

将最后一次迭代存储在循环中(python)

[英]Storing the last iteration in a loop(python)

So I have 2 variables mean and service consisting of the following values: 所以我有2个变量mean和service由以下值组成:

mean=[5.76,6.5,7,5,4.5,6,9,2.5,3,5.5]
limit=[90,80,95,96,98,85,82,75,90,91]

Now,I have applied poisson function on the variable mean: 现在,我在变量均值上应用了poisson函数:

from scipy.stats import poisson
for m in mean:
    r_v=poisson(m)
    r_v1.append(r_v)

Next what I need to do is obtain the sum of pmfs of all the values of mean(with the number of occurrences starting from 0) and compare those to the corresponding values in limit. 接下来我需要做的是获得所有mean值的pmfs的总和(从0开始的出现次数),并将它们与limit中的相应值进行比较。 I need to obtain the number of occurrence for which the sum of pmfs is greater than or equal to the corresponding value in limit. 我需要获取pmfs之和大于或等于limit中相应值的出现次数。 For example, for mean =5.76, (pmf(0)+pmf(1)...+pmf(9))*100=93.2, which exceeds 90 in the limit variable for 9 occurences. 例如,对于平均值= 5.76,(pmf(0)+ pmf(1)... + pmf(9))* 100 = 93.2,其在9个出现的极限变量中超过90。 I've run the following code which seems to work fine: 我运行以下代码似乎工作正常:

for r,s in zip(r_v1, limit):
        l=[]
        z=0
        while True:
            y=r.pmf(z)
            l.append(y)
            z+=1
            if round(sum(l),2)*100>=s:
                break
Store=%z

The issue is, when I'm trying to store the last occurrence of z at which round(sum(l),2)*100>=s , I'm getting the following error: 问题是,当我试图存储最后一次出现的z时( round(sum(l),2)*100>=s ,我得到以下错误:

ERROR:root:Line magic function `%z` not found.

Can somebody please help me rectify the error in the code? 有人可以帮我纠正代码中的错误吗? Thanks a lot. 非常感谢。

% is the prefix for invoking magic line functions within IPython's interactive shell and there is no z line magic function, hence you get this error. %是在IPython的交互式shell中调用魔术行函数的前缀,并且没有z行魔术函数,因此您会收到此错误。

type %quickref in IPython and you get an overview over the available magic functions or readthedocs . 在IPython中键入%quickref ,您可以获得有关可用魔术函数或readthedocs的概述


Your problem was that you were trying too much in one step. 你的问题是你在一步中尝试了太多。 Try getting the right result for one input and then accumulate in another step: 尝试为一个输入获得正确的结果,然后在另一个步骤中累积:

def calc(mean_, limit_):

    def inner(m, s):
        r = poisson(m)
        l = []
        z = 0
        while True:
            y = r.pmf(z)
            l.append(y)
            z += 1
            if round(sum(l), 2) * 100 >= s:
                return z, l

    return [inner(*args) for args in zip(mean_, limit_)]

Calling calc(mean, limit) gives: 调用calc(mean, limit)给出:

[(10,
  [0.0031511115984444414,
   0.018150402807039979,
   0.052273160084275148,
   0.10036446736180832,
   0.14452483300100394,
   0.16649260761715656,
   0.15983290331247035,
   0.13151964615426115,
   0.094694145231068075,
   0.060604252947883679]),
 (10,
  [0.0015034391929775724,
   0.009772354754354215,
   0.031760152951651209,
   0.068813664728577653,
   0.11182220518393866,
   0.14536886673912017,
   0.1574829389673803,
   0.14623415761256733,
   0.11881525306021086,
   0.08581101609904139]),
 (12,
  [0.00091188196555451624,
   0.0063831737588816145,
   0.022341108156085643,
   0.052129252364199796,
   0.091226191637349643,
   0.1277166682922895,
   0.14900277967433773,
   0.14900277967433773,
   0.1303774322150455,
   0.10140466950059107,
   0.070983268650413558,
   0.045171170959354162]),
 (10,
  [0.006737946999085467,
   0.033689734995427337,
   0.084224337488568321,
   0.1403738958142805,
   0.17546736976785063,
   0.17546736976785068,
   0.1462228081398754,
   0.10444486295705395,
   0.065278039348158651,
   0.036265577415643714]),
 (10,
  [0.011108996538242306,
   0.049990484422090385,
   0.11247858994970336,
   0.168717884924555,
   0.18980762054012446,
   0.17082685848611215,
   0.1281201438645839,
   0.082362949627232548,
   0.046329159165318316,
   0.0231645795826592]),
 (9,
  [0.0024787521766663585,
   0.014872513059998144,
   0.044617539179994441,
   0.089235078359988937,
   0.13385261753998332,
   0.16062314104797995,
   0.16062314104798009,
   0.13767697804112569,
   0.10325773353084421]),
 (13,
  [0.00012340980408667956,
   0.0011106882367801166,
   0.0049980970655105232,
   0.014994291196531574,
   0.033737155192196056,
   0.06072687934595293,
   0.091090319018929264,
   0.1171161244529091,
   0.13175564000952278,
   0.13175564000952278,
   0.11858007600857066,
   0.097020062188830414,
   0.072765046641622894]),
 (4,
  [0.0820849986238988,
   0.20521249655974699,
   0.25651562069968376,
   0.21376301724973648]),
 (6,
  [0.049787068367863944,
   0.14936120510359185,
   0.22404180765538775,
   0.22404180765538775,
   0.16803135574154085,
   0.10081881344492458]),
 (10,
  [0.0040867714384640666,
   0.02247724291155237,
   0.06181241800676901,
   0.1133227663457432,
   0.15581880372539689,
   0.17140068409793663,
   0.15711729375644187,
   0.12344930223720431,
   0.084871395288077939,
   0.051865852676047694])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM