[英]Is there any alternative ways other than loop to update the dictionary from any sequence data (Python)?
I have a code to update a dictionary like this : 我有一个代码来更新这样的字典:
c = { }
for i in ID :
d = {i : V[i]}
c.update(d)
Both ID and V are sequence data with a complex and huge items, where ID is a list and V is a dictionary. ID和V都是具有复杂和巨大项目的序列数据,其中ID是列表,V是字典。
Is there any ways in python to do that logic without using loop processes like "for"? python中有没有办法在不使用像“for”这样的循环过程的情况下执行该逻辑?
The use of loop processes take a lot of iteration impacted on run time. 循环过程的使用会在运行时受到很多迭代的影响。
No, you can't avoid a loop but you can try these alternatives: 不,你不能避免循环,但你可以尝试这些替代方案:
c = { }
for i in ID :
c[i] = V[i]
or 要么
c = dict([(i, V[i]) for i in ID])
or 要么
c = {i: V[i] for i in ID}
the short way of your code is: 你的代码的简短方法是:
c.update({i:V[i] for i in ID})
also you could use map
, but it will iterate over 你也可以使用
map
,但它会迭代
c.update(dict(map(lambda i:(i,V[i]),ID)))
Its all O(n) and you could just move it into C
part rather than Python by using above notations! 它的所有O(n)你可以通过使用上面的符号将它移动到
C
部分而不是Python!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.