相关疑难解决方法(0)

Python:List vs Dict查找表

我有大约1000万个值,我需要放在某种类型的查找表中,所以我想知道哪个列表字典更有效?

我知道你可以做两件事:

if something in dict_of_stuff:
    pass
Run Code Online (Sandbox Code Playgroud)

if something in list_of_stuff:
    pass
Run Code Online (Sandbox Code Playgroud)

我的想法是dict会更快更有效率.

谢谢你的帮助.

编辑1
关于我正在尝试做什么的更多信息. 欧拉问题92.我正在查找表,看看计算出的值是否已经准备就绪.

编辑2
查找效率.

编辑3
没有与值相关的值...那么一会更好吗?

python performance

158
推荐指数
7
解决办法
14万
查看次数

如何优化计算python列表中的元素

这几乎超过了同样的问题在这里,但我问了排序结果的最有效的解决方案.

我有一个列表(大约10个整数在0到12之间随机),例如:

the_list = [5, 7, 6, 5, 5, 4, 4, 7, 5, 4]
Run Code Online (Sandbox Code Playgroud)

我想创建一个函数,该函数返回由第一个元素排序的元组(项目,计数)列表

output = [(4, 3), (5, 4), (6, 1), (7, 2)]
Run Code Online (Sandbox Code Playgroud)

到目前为止我用过:

def dupli(the_list):
    return [(item, the_list.count(item)) for item in sorted(set(the_list))]
Run Code Online (Sandbox Code Playgroud)

但我把这个函数称为几乎是一个时间,我需要像我(python)一样快.因此我的问题是:如何让这个功能减少时间消耗?(内存怎么样?)

我玩了一下,但没有明显的结果:

from timeit import Timer as T
number=10000
setup = "the_list=[5, 7, 6, 5, 5, 4, 4, 7, 5, 4]"

stmt = "[(item, the_list.count(item)) for item in sorted(set(the_list))]"
T(stmt=stmt, setup=setup).timeit(number=number)

Out[230]: 0.058799982070922852

stmt = "L = []; \nfor item in sorted(set(the_list)): …
Run Code Online (Sandbox Code Playgroud)

python performance list count

6
推荐指数
1
解决办法
8965
查看次数

标签 统计

performance ×2

python ×2

count ×1

list ×1