使用嵌套的for循环创建字典

时间:2019-05-13 17:15:11

标签: python loops dictionary nested

我正在寻找从嵌套循环创建一个字典,该字典循环访问日期和变量。考虑到我的约会时间是〜400和myvars〜50,000,我想尽可能高效地创建它。非常感谢。

fmap = dict.fromkeys(range(len(dates)*len(myvars)))
count = 0
for j in dates:
    for i in myvars:
        fmap[count] = partial(dosomething, i, j)
        count = count+1

1 个答案:

答案 0 :(得分:1)

您可以执行此操作,它应该非常有效:

count = len(dates) * len(myvars)
fmap = dict(zip(range(count), [partial(dosomething, i, j) for j in dates for i in myvars]))