我有一个非常简单的Python脚本来创建(用于测试目的),列表中有3500万个字典对象.每个字典对象包含两个键/值对.例如.
{'Name': 'Jordan', 'Age': 35}
Run Code Online (Sandbox Code Playgroud)
该脚本非常简单地对名称和年龄进行查询,搜索字典列表并返回包含所有匹配字典条目索引的新列表.
但是,如下所示,消耗了大量内存.我认为我在某个地方犯了一个非常天真的错误.

我的代码如下:(如果更具可读性,也可以在图像中查看).
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)