我有一个非常大的python脚本,200K,我想尽可能少使用内存.它看起来像:
# a lot of data structures
r = [34, 78, 43, 12, 99]
# a lot of functions that I use all the time
def func1(word):
return len(word) + 2
# a lot of functions that I rarely use
def func1(word):
return len(word) + 2
# my main loop
while 1:
# lots of code
# calls functions
Run Code Online (Sandbox Code Playgroud)
如果我把我很少使用的函数放在模块中,并且只在必要时动态导入它们,我就无法访问数据.就我而言,这就是我的意思.
我是python的新手.
任何人都可以把我放在正确的道路上吗?如何打破这个大脚本,以便它使用更少的内存?是否值得将很少使用的代码放在模块中,只在需要时调用它们?
a = [3,5,8,3,9,5,0,3,2,7,5,4]
for o in a[::3]:
print o
Run Code Online (Sandbox Code Playgroud)
这让我得到了第一个和每3个项目.3,3,0,7
有没有办法可以检索下两个项目?
a = [3,5,8,3,9,5,0,3,2,7,5,4]
for o in a[::3]:
if o == 0:
print o
print o + 1
print o + 2
Run Code Online (Sandbox Code Playgroud)
输出0 3 2
我知道这不正确,但也许你可以看到我想要做的事情.基本上,我有一长串属性,每个属性有三个部分,parent_id,property_type和property_value,我需要从列表中检索属性的所有三个部分.