将可检索的唯一 ID 分配给 python 中不断变化的 lambda 列表?

发布于 2024-10-03 18:21:56 字数 2494 浏览 5 评论 0原文

def a(p): return p + 1

def b(p): return p + 2

def c(p): return p + 3

l= [a,b,c]

import itertools
ll = itertools.combinations(l, 2)

[x for x in ll]
[(<function a at 0x00CBD770>, <function b at 0x00CBD7F0>),
 (<function a at 0x00CBD770>, <function c at 0x00BB27F0>),
 (<function b at 0x00CBD7F0>, <function c at 0x00BB27F0>)]

Q1:在这里,如何用简单的行返回 lambda 列表:

[a(b(1)),  # not the result of a(b(1)), but just a lambda object
 a(c(1)),  # also items may more than 2 here if itertools.combinations(l, 4)
 b(c(1))]

Q2:

假设我定义了另一个函数,

def d(p): return p + 4

l= [a,b,c,d]
ll = itertools.combinations(l, 2)

[(<function a at 0x00CBD770>, <function b at 0x00CBD7F0>),
 (<function a at 0x00CBD770>, <function c at 0x00BB27F0>),
 (<function a at 0x00CBD770>, <function d at 0x00CBDC70>),
 (<function b at 0x00CBD7F0>, <function c at 0x00BB27F0>),
 (<function b at 0x00CBD7F0>, <function d at 0x00CBDC70>),
 (<function c at 0x00BB27F0>, <function d at 0x00CBDC70>)]

该组合具有与上一个序列相比不同的序列:

ab,ac,ad,bc,bd,cd
=================
ab,ac,bc

但我想保留所有可能的项目具有唯一的ID,这意味着无论如何

l= [a,b,c,d] 

l= [b,a,c,d] 

pr

l= [a,b,e,d] 

“ac”为例:“ac”和其他可能的项目始终使用唯一的 ID 绑定,然后我可以使用该唯一的 ID 访问“ac”。我认为这就像为每个项目创建一个可扩展的哈希表。

那么,是否可以为 lambda 项分配一个 int ID 或“HASH”?我还希望这种映射关系应该能够作为文件存储在磁盘中并且可以稍后检索。

谢谢你的任何想法。

解释 Q2 的示例

=====================
l= [a,b,c,d] 
func_combos = itertools.combinations(l, 2)
compositions = [compose(f1, f2) for f1, f2 in func_combos] 

[compositions[x](100) for x in compositions]  # take very long time to finish
[result1,
 result2,
 result3,
 ... 
 ]

======== three days later on another machine ======
l= [a,c,b,e,f,g,h] 
[compositions[x](100) for x in compositions] # take very long time to finish

[newresult1,
 newresult2,
 newresult3,
 ... 
 ]

but wait: here we can saving time: take "ac" for example:

[result1, tag
 result2, tag_for_ac_aka_uniqueID_or_hash
 result3, tag
 ... 
 ]

we just need to check if the "ac" tag exists we can reduce the calculation:
if hash_of(ac) in list(result.taglist):
   copy result to new result:
def a(p): return p + 1

def b(p): return p + 2

def c(p): return p + 3

l= [a,b,c]

import itertools
ll = itertools.combinations(l, 2)

[x for x in ll]
[(<function a at 0x00CBD770>, <function b at 0x00CBD7F0>),
 (<function a at 0x00CBD770>, <function c at 0x00BB27F0>),
 (<function b at 0x00CBD7F0>, <function c at 0x00BB27F0>)]

Q1: here, how to return a lambda list in simple line(s):

[a(b(1)),  # not the result of a(b(1)), but just a lambda object
 a(c(1)),  # also items may more than 2 here if itertools.combinations(l, 4)
 b(c(1))]

Q2:

suppose I defined another function d

def d(p): return p + 4

l= [a,b,c,d]
ll = itertools.combinations(l, 2)

[(<function a at 0x00CBD770>, <function b at 0x00CBD7F0>),
 (<function a at 0x00CBD770>, <function c at 0x00BB27F0>),
 (<function a at 0x00CBD770>, <function d at 0x00CBDC70>),
 (<function b at 0x00CBD7F0>, <function c at 0x00BB27F0>),
 (<function b at 0x00CBD7F0>, <function d at 0x00CBDC70>),
 (<function c at 0x00BB27F0>, <function d at 0x00CBDC70>)]

this combination with different a sequence compare with last one :

ab,ac,ad,bc,bd,cd
=================
ab,ac,bc

But I want to keep all possible item with an unque ID, it means no matter how the

l= [a,b,c,d] 

or

l= [b,a,c,d] 

pr

l= [a,b,e,d] 

Take "ac" for example: the "ac" and other possible item always with an unique ID bind then I can access "ac" with that unique ID. I think it is like to create an extendable hash table for each item.

So, is it possible to assign an int ID or a "HASH" to the lambda item? I also want this mapping relationship should be able to store in disk as a file and can be retrieved later.

Thanks for any idea.

sample to explain Q2

=====================
l= [a,b,c,d] 
func_combos = itertools.combinations(l, 2)
compositions = [compose(f1, f2) for f1, f2 in func_combos] 

[compositions[x](100) for x in compositions]  # take very long time to finish
[result1,
 result2,
 result3,
 ... 
 ]

======== three days later on another machine ======
l= [a,c,b,e,f,g,h] 
[compositions[x](100) for x in compositions] # take very long time to finish

[newresult1,
 newresult2,
 newresult3,
 ... 
 ]

but wait: here we can saving time: take "ac" for example:

[result1, tag
 result2, tag_for_ac_aka_uniqueID_or_hash
 result3, tag
 ... 
 ]

we just need to check if the "ac" tag exists we can reduce the calculation:
if hash_of(ac) in list(result.taglist):
   copy result to new result:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2024-10-10 18:21:56

只是使用 set 来避免字典?

它们对我来说是作品。

just use set to avoid dicts?

They are works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文