相关疑难解决方法(0)

许多词典使用大量的RAM

我有一个非常简单的Python脚本来创建(用于测试目的),列表中有3500万个字典对象.每个字典对象包含两个键/值对.例如.

{'Name': 'Jordan', 'Age': 35}
Run Code Online (Sandbox Code Playgroud)

该脚本非常简单地对名称和年龄进行查询,搜索字典列表并返回包含所有匹配字典条目索引的新列表.

但是,如下所示,消耗了大量内存.我认为我在某个地方犯了一个非常天真的错误.

代码和任务管理器的屏幕截图显示ram使用情况

我的代码如下:(如果更具可读性,也可以在图像中查看).

import sys

# Firstly, we will create 35 million records in memory, all will be the same apart from one

def search(key, value, data, age):
    print("Searching, please wait")
    # Create list to store returned PKs
    foundPKS = []
    for index in range(0, len(data)):
        if key in data[index] and 'Age' in data[index]:
            if data[index][key] == value and data[index]['Age'] >= age:
                foundPKS.append(index)
    results = foundPKS
    return results

def createdata():
    # Let's create …
Run Code Online (Sandbox Code Playgroud)

python memory optimization dictionary python-3.x

4
推荐指数
1
解决办法
187
查看次数

标签 统计

dictionary ×1

memory ×1

optimization ×1

python ×1

python-3.x ×1