如何在字典的子集上进行迭代?
我是编码的新手,但是我有一个现实生活中的问题,我想拥有一些代码。我有一个项目列表,每个项目都有位置 - 我将其放入python中,例如objects = {“ a”:0,“ B”:7,“ C”:24,“ D”:2}
如何循环浏览此词典并找到每对项目的中点?我缩写为“ x/y”。因此,最终我想要看起来像Midpoints = {“ A/B”:3.5,“ A/C”:12,“ A/D”:1,“ B/C”:15.5,“ B/ D“:4.5,” C/D“:13}
。
我知道您将从函数和一个循环开始,也许是类似的东西
def mp(x,y):
return (x+y)/2
midpoints = {}
for key1 in objects:
for key2 in objects:
midpoints[key1+"/"+key2] = mp(objects[key1],objects[key2])
,但是这给出了无用的中点(“ a/a”)以及重复的中点(例如“ a/b”和“ b/a”) 。
我不知道如何清理……我想您必须在字典的一部分上迭代?任何帮助将不胜感激! :)
I am very new to coding, but I have a real-life problem that I want to have some code for. I have a list of items, which each have locations - I put it into Python as something like objects = {"a": 0, "b": 7, "c": 24, "d": 2}
How do I loop through this dictionary and find the midpoints of each pair of items? I abbreviate midpoint as "x/y". So ultimately I want something that looks like, midpoints = {"a/b": 3.5, "a/c": 12, "a/d": 1, "b/c": 15.5, "b/d": 4.5, "c/d": 13}
.
I know you'd start with a function and a for loop, maybe something like
def mp(x,y):
return (x+y)/2
midpoints = {}
for key1 in objects:
for key2 in objects:
midpoints[key1+"/"+key2] = mp(objects[key1],objects[key2])
but this is giving useless midpoints ("a/a") as well as repeated midpoints (e.g. both "a/b" and "b/a").
I can't figure out how to clean this up...I guess you'd have to iterate over a subset of the dictionary? Any help would be greatly appreciated! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在寻找A 笛卡尔产品。您的实现几乎是正确的,但是
中点[key1+“/”+key2] = z
行需要缩进为每对(key1,key2)
运行。如果您现在
打印(中点)
,您将获得{'a/a':0.0,'a/b':3.5,'a/c':12.0,'a/a/ D':1.0,'b/a':3.5,'b/b':7.0,'b/c':15.5,'b/d':4.5,'c/a':12.0,'c/b' :15.5,'c/c':24.0,'c/d':13.0,'d/a':1.0,'d/b':4.5,'d/c':13.0,'d/d':2.0 }
。从您的示例中,我推断您要简化这一点:X/X
不应出现在最终字典中;X/Y
和y/x
只能以一种实现此目的的一种方法出现一个是需要
key1< key2
(词汇比较):这与您的示例相似。如果要包括
X/X
,只需更改key1< key2
tokey1< = key2
。You're looking for a cartesian product. Your implementation is almost correct, but the
midpoints[key1+"/"+key2] = z
line needs to be indented to run for every pair(key1, key2)
.if you now
print(midpoints)
, you'll obtain{'a/a': 0.0, 'a/b': 3.5, 'a/c': 12.0, 'a/d': 1.0, 'b/a': 3.5, 'b/b': 7.0, 'b/c': 15.5, 'b/d': 4.5, 'c/a': 12.0, 'c/b': 15.5, 'c/c': 24.0, 'c/d': 13.0, 'd/a': 1.0, 'd/b': 4.5, 'd/c': 13.0, 'd/d': 2.0}
. From your example I infer that you want to simplify this:x/x
should not appear in the final dictionary;x/y
andy/x
should appear in the dictone way to achieve this would be to require
key1 < key2
(lexical comparison):this yields a dict similar to the one from your example. If you want to include
x/x
, just changekey1 < key2
tokey1 <= key2
.您可以使用
itertools.combinationss.combinations
:打印:
You can use
itertools.combinations
:Prints:
要避免重复项,请使用
枚举
获得索引,然后通过内部环中的对象的切片列表进行迭代:或者使用
itertools.combinations
,它在不重复的情况下生成了所有峰值的组合:To avoid duplicates, use
enumerate
to obtain an index, and iterate through a sliced list of the objects in the inside loop:Alternatively use
itertools.combinations
which generates all combinations of an iterable without repetition:您也许可以尝试以下类似的东西。逻辑是将嵌套的循环用于两个点,然后使用IF条件仅在Numerator和分母为不同点的情况下添加值。
You can perhaps try something like the below. The logic is to use a nested for loop for the two points, then use an if condition to only add in the value if numerator and denominator would be different points.