利用python itertools对列表实现排列组合

import itertools


lst = [112, 123, 98, 132, 120]

list(itertools.permutations(lst,2))

# 输出:
# [(112, 123),
#  (112, 98),
#  (112, 132),
#  (112, 120),
#  (123, 112),
#  (123, 98),
#  (123, 132),
#  (123, 120),
#  (98, 112),
#  (98, 123),
#  (98, 132),
#  (98, 120),
#  (132, 112),
#  (132, 123),
#  (132, 98),
#  (132, 120),
#  (120, 112),
#  (120, 123),
#  (120, 98),
#  (120, 132)]

list(itertools.combinations(lst, 2))

# 输出:
# [(112, 123),
#  (112, 98),
#  (112, 132),
#  (112, 120),
#  (123, 98),
#  (123, 132),
#  (123, 120),
#  (98, 132),
#  (98, 120),
#  (132, 120)]
作者:jsp

-------------------------------------------

个性签名:无论在哪里做什么,只要坚持服务、创新、创造价值,其它的东西自然都会来的。

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

原文地址:https://www.cnblogs.com/jingsupo/p/14962083.html