您好我是Python的新手,我有一个简单的功能,如下所示:
def normal_list(p, data, n):
cost = 0
cost_list = []
list = []
clappend=cost_list.append
listappend=list.append
for i in range(n):
x = np.random.choice(data, p=p)
if (len(list) == 0):
listappend(x)
else:
for i in list:
if i == x:
clappend(cost)
elif i == list[-1]:
listappend(x)
cost += 1
cost = 0
return cost_list
其中p是概率列表,数据是几乎所有情况下的数据列表,它将是数字1-100的列表我必须加快这一点,因为我要使用此函数n = 100000然后它需要很长时间cost_list的内存错误的时间和结束。感谢所有建议,我发现了我的错误。在我的列表中找到x之后,我搞砸了我没有打破循环。所以再次感谢并使用生成器是一个好主意。
yield
而不是return将通过“动态”产生结果而不是在返回之前收集所有内容来节省内存。
这可能不是上面使用生成器的完全功能实现,但它是一个开始:
def sub1(lst, x):
cost = 0
for e in lst:
cost += 1
if e == x:
yield (cost, None)
elif e == lst[-1]:
yield (None, x)
def normal_list(p, data, n):
lst = []
for i in range(n):
x = np.random.choice(data, p=p)
if len(lst) == 0:
yield x
else:
for res in sub1(lst, x):
if res[0] is not None:
yield res[0]
else:
lst += res[1]
您可以消除外部条件检查。你的list
只有一次空:在第一次迭代之前。为什么不立即初始化并跳过外部循环的第一次迭代并完全检查:
def normal_list(p, data, n):
cost = 0
cost_list = []
_list = [np.random.choice(data, p=p)]
#clappend=cost_list.append # Don't do this! It's confusing!
#listappend=_list.append(x) # Don't do this! It's confusing!
for i in range(1,n):
...