相关疑难解决方法(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万
查看次数

计算/创建十的幂的最快方法是什么?

如果您提供(整数)幂作为输入,那么创建相应的 10 次幂的最快方法是什么?我可以提出以下四种替代方案,最快的方法似乎是使用 f 字符串:

from functools import partial
from time import time
import numpy as np

def fstring(power):
    return float(f'1e{power}')

def asterisk(power):
    return 10**power

methods = {
    'fstring': fstring,
    'asterisk': asterisk,
    'pow': partial(pow, 10),
    'np.pow': partial(np.power, 10, dtype=float)
}

# "dtype=float" is necessary because otherwise it will raise: 
# ValueError: Integers to negative integer powers are not allowed.
# see https://stackoverflow.com/a/43287598/5472354
powers = [int(i) for i in np.arange(-10000, 10000)]
for name, method in methods.items():
    start = time()
    for i …
Run Code Online (Sandbox Code Playgroud)

python

7
推荐指数
1
解决办法
7355
查看次数

标签 统计

python ×2

performance ×1