gpt4 book ai didi

python - 在字典中迭代..(双重迭代)python

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:54 25 4
gpt4 key购买 nike

我有一本字典

example: A = {1:'one',2:'two' , 3: 'three}

所以我想要的基本上就像一个 2 for 循环之类的东西..这样我就可以获得以下订单..

# 1 2
# 1 3
# 2 3
... and so on if more elements are there

所以基本上是 o(n2) 操作..我们有一个循环和一个循环我们如何在 python 字典中实现这一点。我很难弄清楚..

for key in A.keys():
# how do i Access all the other keys..
# do something

谢谢

最佳答案

>>> import itertools as it
>>> A = {1:'one', 2:'two', 3: 'three'}
>>> list(it.combinations(A.keys(), 2))
[(1, 2), (1, 3), (2, 3)]

关于python - 在字典中迭代..(双重迭代)python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8439042/

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