gpt4 book ai didi

dictionary - 当有值时,我可以在 tcl dict 中查找一个键吗?反向查找

转载 作者:行者123 更新时间:2023-12-04 17:19:53 29 4
gpt4 key购买 nike

我有一个包含键及其值的 tcl 字典。有没有办法进行“反向查找”,例如查找值并检索 key 。我知道这听起来不像是最先进的编程方式,但是我的代码中已经存在字典,我不想仅仅因为这一次我需要它而以另一种方式重新创建它。

最佳答案

您可以使用 dict filter 命令。值可以重复,因此您应该期望有多个键。

set d [dict create a b c d e b]
# note that the "b" value is repeated for keys "a" and "e"

dict filter $d value b
-> a b e b

所以你可以这样使用:

set lookupVal b
dict for {k v} [dict filter $d value $lookupVal] {
lappend keys $k
}

puts $keys
-> a e

关于dictionary - 当有值时,我可以在 tcl dict 中查找一个键吗?反向查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20542205/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com