跟踪结果从一个Python循环的最佳方式 [英] Best way to keep track of results from a Python loop

查看:99
本文介绍了跟踪结果从一个Python循环的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的循环,需要运行500次,我是新来使用的编程语言,做这种类型的仿真。

但我需要记录每次运行的结果,如果列表(表1)包含全部为0,全部为1或两者的混合。

我只是想知道用什么方法会以最快的速度找出了500仿真比例,导致包含列表中的所有0,全1或混合,如果追加会慢下来太多了。

 在范围X(0,500):    次= 300
    gamma_val = 2    表1 = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]    总= 0.0
    而总<时间:
        表= [在范围0我(21)
        为D1的范围(21):
            如果table1的[D1]:
               表[D1] = - (1 / gamma_val)*将Math.log((random.random()))
            其他:
               表[D1] = - (将Math.log(random.random()))
      ####那张做出新的表1与变化,无论是全1,全0或0的混合#########和1    文件下载1​​ = 0
    files01 = 0
    files0 = 0    如果1,0,在表1 ==真:
             files01 + = 1
    ELIF 1表==真:
             文件下载1​​ + 1 =
    ELIF 0在表1 ==真:
             files0 + = 1


解决方案

有)不敌没有速度追加(,相比在东西多失去了扫描速度倍。还有就是输给了比计算的内存操作的时间非常少。所以不要担心。我可能不会保持计数,如果我能向你仍要积累名单的长度得到他们。这将更加有可读性一次做的一切。做的东西,再算上。

我相信相对于约算法的速度作出决策的标准集装箱。所以我会投行到Set,并比较设置([0]),集([1])和套装([0,1])。我认为'在'会做该行的双重扫描,而套装()将使一遍。

  BOTH =集([0,1])
结果= {'0':[],'1':[],'01':[]}....榜上无名....
元素=集(表)
如果要素==两种:
    结果['01']。追加(表)
否则,如果1部分:
    结果['1']。追加(表)
其他:
    结果[0]。追加(表)

和我会努力让我对风格采摘,不是彻底错误:<​​/ P>

使用最后的东西,并没有穷尽所有的条件;它确实是更具可读性。用尽他们都分别做读者想知道你想象是什么情况下可能会被留下。它孕育着偏执狂。

在一般情况下,比较实际的事情为真或假非Python的。检查平等恒真是最可靠的方式来获得你想要的Python中的条件是什么。有很多依赖于被重新由对象或列表的非emptynes​​s存在psented $ P $声明的真实性Python的成语。所以,你将面对谁比返回True或从辅助函数虚假重新present布尔确定其他一些程序员。习惯了不被挑剔了。

此外,由于痛苦似乎与其他比较符链混合,尽管这意味着&LT; ,它是如此的不地道的非分析师表示,你应该的从不的做到这一点。

I have a fairly big loop that needs to run 500 times, and I'm new to using the programming language and doing this type of simulation.

But I need to document the results of each run, and if the list (table1) contains either all 0's, all 1's or a mix of both.

I was just wondering what method would be the fastest to find out what proportion of the 500 simulations, resulted in a list that contained all 0's, all 1's or a mix and if append would slow it down too much.

for x in range(0, 500):

    times = 300
    gamma_val = 2

    table1 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    total = 0.0
    while total < times:
        table = [0 for i in range (21)]
        for d1 in range(21):
            if table1[d1]:
               table[d1] = -(1/gamma_val)*math.log((random.random()))
            else:
               table[d1] = -(math.log(random.random()))
      ####Goes on to make new table1 with changes, either all 1's, all 0's or a mix of 0s   #########and 1s

    files1 = 0
    files01 = 0
    files0 = 0

    if "1" "0" in table1 == True:
             files01 += 1
    elif 1 in table == True:
             files1 += 1
    elif 0 in table1 == true:
             files0 += 1

解决方案

There is no speed lost to append(), compared to the speed lost to scanning over things multiple times. And there is very little time lost to memory operations compared to the computations. So don't worry about it. I might not keep the counts, if I can get them from the lengths of the lists you want to accumulate anyway. It is more readable to do everything once. Make the stuff, and then count.

I trust the standard containers relative to making decisions about speed of algorithms. So I would cast the row to a Set and compare to the Set([0]), Set([1]), and Set([0,1]). I assume 'in' will do a double scan of the row, whereas Set() will make one pass.

BOTH = set([0, 1])
results = {'0': [], '1': [], '01': []}

.... make the list ....


elements = set(table)
if elements == BOTH:
    results['01'].append(table)
else if 1 in elements:
    results['1'].append(table)
else:
    results['0'].append(table)

And I will try to make my picking about style, not outright errors:

Use the final else, don't exhaust all the conditions; it really is more readable. Exhausting them all separately makes readers wonder what case you imagine might be left. It breeds paranoia.

In general, actually comparing things to True or False is non-pythonic. Checking equality to the constant True is the least reliable way to get what you want out of an condition in Python. There are a lot of Python idioms that rely on the truth of a statement being represented by the existence of an object, or the non-emptyness of a list. So you will face programmers who return something other than True or False from helper functions to represent Boolean determinations. Get used to not being finicky about it.

Also, painful as it seems, in when mixed with other comparison operators chains as though it meant <, which is so non-idiomatic for non-analysts that you should never do it.

这篇关于跟踪结果从一个Python循环的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆