小编gun*_*gor的帖子

为什么'new_file + = line + string'比'new_file = new_file + line + string'快得多?

当我们使用时,我们的代码需要10分钟来虹吸68,000条记录:

new_file = new_file + line + string
Run Code Online (Sandbox Code Playgroud)

但是,当我们执行以下操作时,只需1秒钟:

new_file += line + string
Run Code Online (Sandbox Code Playgroud)

这是代码:

for line in content:
import time
import cmdbre

fname = "STAGE050.csv"
regions = cmdbre.regions
start_time = time.time()
with open(fname) as f:
        content = f.readlines()
        new_file_content = ""
        new_file = open("CMDB_STAGE060.csv", "w")
        row_region = ""
        i = 0
        for line in content:
                if (i==0):
                        new_file_content = line.strip() + "~region" + "\n"
                else:
                        country = line.split("~")[13]
                        try:
                                row_region = regions[country]
                        except KeyError:
                                row_region = "Undetermined"
                        new_file_content += …
Run Code Online (Sandbox Code Playgroud)

python string cpython string-concatenation python-internals

8
推荐指数
2
解决办法
1108
查看次数

与dict()相比,Python OrderDict溅射

这个让我感到困惑.

asset_hist = []
for key_host, val_hist_list in am_output.asset_history.items():
    for index, hist_item in enumerate(val_hist_list):
        #row = collections.OrderedDict([("computer_name", key_host), ("id", index), ("hist_item", hist_item)])
        row = {"computer_name": key_host, "id": index, "hist_item": hist_item}
        asset_hist.append(row)
Run Code Online (Sandbox Code Playgroud)

此代码与注释掉的集合行完美配合.但是,当我注释掉row = dict行并从集合行中删除注释时,事情变得非常奇怪.这些行中大约有400万个生成并附加到asset_hist.

因此,当我使用row = dict时,整个循环在大约10毫秒内完成,闪电般快速.当我使用有序词典时,我等了10多分钟,但仍然没有完成.现在,我知道OrderDict应该比dict慢一点,但它应该在最坏的情况下慢大约10倍,而我的数学实际上它在这个函数中慢了大约100,000倍.

我决定在最低的循环中打印索引,看看发生了什么.有趣的是,我注意到控制台输出中的溅射.索引将在屏幕上快速打印,然后停止约3-5秒,然后继续.

am_output.asset_history是一个字典,它有一个键,主机,每一行都是一个字符串列表.例如

am_output.asset_history = {"host1":["string1","string2",...],"host2":["string1","string2",...],...}

编辑:使用OrderedDict进行溅射分析

此VM服务器上的总内存:仅8GB ...需要更多的提供.

LOOP NUM

184796(约5秒等待,约60%内存使用)

634481(等待约5秒,内存使用率约为65%)

1197564(约5秒等待,约70%内存使用)

1899247(约5秒等待,约75%内存使用)

2777296(约5秒等待,约80%内​​存使用)

3873730(LONG WAIT ......等了20分钟放弃了!,88.3%的内存使用率,进程仍然在运行)

等待发生的地方随着每次运行而变化.

编辑:再次跑,它这次停在3873333,接近它之前停止的位置.在形成行之后它停止了,同时试图追加......我没有注意到最后一次尝试但它也在那里......问题在于追加线,而不是行线......我仍然百思不得其解.这是在长停止之前生成的行(将行添加到print语句中)...更改主机名以保护无辜:

3873333:OrderedDict([('computer_name','bg-fd5612ea'),('id',1),('hist_item',"sys1 Normalizer(sys1-4):无法从sys1名称确定域名'bg- fd5612ea"".)])

python loops ordereddictionary

5
推荐指数
1
解决办法
148
查看次数

你如何在python3程序中使用python2模块

我在python3中编写了一个40k的行程序.现在我需要在我的程序中使用一个名为pytan的模块,它将赋予功能添加.问题是pytan是用python2编写的.

那么有可能在一个脚本中将解释器切换到python 2.7,而另一个脚本在python 3中运行吗?

处理这种情况的最佳方法是什么.

python python-2.7 python-3.x

3
推荐指数
1
解决办法
84
查看次数