如何解决python:如何创建以元组形式为A和B中的整数生成所有可能组合的东西
问题:
# Let A= {m | m is an integer satisfying 0 < m < 13} and
# B = {n | n is an integer satisfying 7 < n < 23}.
# I'm trying to generate all possible combinations of (A,B).
For example:
A= (1,2,3,4,5,6,7)
B =(7,8,9,10,11...)
Combination = (1,7),(1,8)(1,9)....(2,(2,8),9).... and so forth
-我当时在考虑使用for循环,但似乎无法使其正常工作。
解决方法
使用itertools.product
,如以下代码所示:
import itertools
out = itertools.product(A,B)
,
为简单起见,您可以使用:
for a in A:
for b in B:
print((a,b,))
或
combinations = [(a,) for a in A for b in B]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。