如何创建对形式的python字典键

fel*_*yot 0 python algorithm dictionary list

例如,我有一个字典词典

my_dictionary= {'a': 20, 'c': 60, 'b': 10}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我有来自字典的3个键.我想制作对并将它们作为元组存储在列表中.对于上述情况,我会有这个

[(a,b),(a,c),(b,c)]
Run Code Online (Sandbox Code Playgroud)

注意:不重复.

在没有使用任何外部模块的情况下,最好的方法是什么.

谢谢你的贡献.

jam*_*lak 6

>>> from itertools import combinations
>>> d = {'a': 20, 'c': 60, 'b': 10}
>>> list(combinations(d, r=2))
[('a', 'c'), ('a', 'b'), ('c', 'b')]
Run Code Online (Sandbox Code Playgroud)

  • @jamylak很可能是因为它是作业=) (4认同)