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

 

posted @ 2021-07-02 10:08  jingsupo  阅读(222)  评论(0编辑  收藏  举报

欢迎光临