【发布时间】:2013-11-27 18:41:18
【问题描述】:
此代码按预期工作,但占用大量内存并且运行时间比我的代码的任何其他部分要长得多。
def function(input1, input2):
mapping = []
for item in input1:
risks = {"A":0, "B":0, "C":0, "D":0, "E":0}
temp = []
for row in input2:
if item in row[0]:
for key in risks.keys():
if row[1] == key:
risks[key] += 1
temp.append(item)
for key in risks.keys():
temp.append(risks[key])
mapping.append(temp)
return mapping
我希望找到一种更有效且内存更少的方法。 input1 是唯一字符串列表,input2 是非唯一元组列表。必须有更好的方法来做到这一点。
感谢您的帮助。
【问题讨论】:
-
你能解释一下(用语言)你的功能应该做什么吗?
-
或者提供某种输入输出示例……
标签: python performance list compare