简体   繁体   English

将字典值和输出乘以'列表类型'的最快方法

[英]Fastest Way to Multiply Dictionary Values and Output to 'list type'

I have two dictionaries with the same keys but different values. 我有两个具有相同键但不同值的词典。 I want to perform an operation that combines the two and then creates a 'list type' object of them in no specific order. 我想执行一个将两者结合起来的操作,然后按特定顺序创建它们的“列表类型”对象。 I am going to be doing this on large datasets so speed is a priority. 我将在大型数据集上执行此操作,因此速度是首要任务。 What is the fastest way of doing this? 这样做的最快方法是什么?

So far I have these ideas. 到目前为止,我有这些想法。 I am new to python so I don't know much about how each function operates but I am assuming option 2 is the fastest. 我是python的新手,所以我不太了解每个函数如何运行,但我假设选项2是最快的。 Are there faster methods? 有更快的方法吗?

mydict = {"cats":1,"dog":3,"bird":2,"wolves":10}
exp = {"cats":10,"dog":23,"bird":34,"wolves":43}

#Option 1
mydict.update((x, y**(exp[x])) for x, y in mydict.items())
items = mydict.values()

#Option 2          // I think this is currently the fastest
items = []
items.append(y**(exp[x]) for x,y in mydict.items())

#Option 3
items = numpy.array(len(mydict))
for x,y in mydict:
    items[?] = y**(exp[x])     // Is there a way to do this? Maybe with a counter

How about 怎么样

[mydict[key]**exp[key] for key in mydict.keys()]

assuming mydict and exp both have the exact same keys? 假设mydict和exp都有完全相同的键?

Side note: 边注:

[mydict[key]**exp[key] for key in mydict]

does the same thing as above, the .keys() is optional. 做与上面相同的事情, .keys()是可选的。

And if you're concerned with speed use the time module to test the speed of each method as such: 如果您关注速度,请使用时间模块来测试每种方法的速度:

import time
t = time.clock()
for i in range(1000):
    # your code here, for example:
    [mydict[key]**exp[key] for key in mydict.keys()]
print "time", time.clock() - t

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 将类型不是字符串的字典值相乘 - Python - Multiply Dictionary Values where type is not String 将 2 个 Pandas 列彼此相乘并获得值总和的最快方法 - Fastest way to multiply 2 Pandas columns with each other and get the sum of the values 从列表中填充字典的最快方法? - Fastest way to fill dictionary from list? 寻找将字典键(基于值)划分为列表列表的最快方法,以便每个列表不超过某个阈值 - Looking for the fastest way to divide dictionary keys (based on values) into lists of list such that each list does not cross a certain threshold 通过列表中的值引用列表中字典的最快方法? - Fastest way to refer to a dictionary in a list by a value in it? 如果字典的值是列表,如何将字典的值相乘或相加 - How to multiply or add the values of a dictionary if the value of the dictionary is a list Python - 将列表的字典值相乘并将结果存储回不同的字典中? - Python - multiply dictionary values for a list and store the result back in a different dictionary? 在字典中的字典中返回特定列表的最快方法是什么? - What is the fastest way to return a specific list within a dictionary within a dictionary? 从稀疏字典和键列表构建列表的最快方法 - Fastest way to build a list from a sparse dictionary and a list of keys 从字典键中的列表中获取列表的最快和最简单的方法 - Fastest and simplest way of getting a list from list in keys of a dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM