利用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)]

 

users = ['user1', 'user2', 'user3', 'user4']
passwords = ['pass1', 'pass2', 'pass3', 'pass4']

list(itertools.product(users, passwords))

# 输出:
# [('user1', 'pass1'),
#  ('user1', 'pass2'),
#  ('user1', 'pass3'),
#  ('user1', 'pass4'),
#  ('user2', 'pass1'),
#  ('user2', 'pass2'),
#  ('user2', 'pass3'),
#  ('user2', 'pass4'),
#  ('user3', 'pass1'),
#  ('user3', 'pass2'),
#  ('user3', 'pass3'),
#  ('user3', 'pass4'),
#  ('user4', 'pass1'),
#  ('user4', 'pass2'),
#  ('user4', 'pass3'),
#  ('user4', 'pass4')]

 

作者:jsp

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

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

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

posted @ 2021-07-02 10:08  jingsupo  阅读(69)  评论(0编辑  收藏  举报
我要啦免费统计