如何打印列表中两个产品相乘之间可能的每次迭代?

互联网 行业动态 更新时间:2024-06-13 00:19:53

Ann*_*Zen 5

您可以尝试binations_with_replacement()内置模块中的方法,itertools

from itertools import binations_with_replacement

for a, b in binations_with_replacement(range(6), 2):
    if (a ** 2 + b ** 2) < 12:
        print(a, b)

输出:

0 0
0 1
0 2
0 3
1 1
1 2
1 3
2 2

binations_with_replacement()方法与该方法类似binations(),不同之处在于该binations_with_replacement()方法允许在同一组合中多次使用一个元素。

请注意,它不包括1 0作为解决方案,因为0 1结果中已经存在。如果要包含此类,可以使用以下product()方法:

from itertools import product

for a, b in product(range(6), repeat=2):
    if (a ** 2 + b ** 2) < 12:
        print(a, b)

输出:

0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 2
3 0
3 1

更多推荐

两个,迭代,列表中,产品

本文发布于:2023-04-21 21:11:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/hyzx/ec947a39097667082847039bf5af7de0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   迭代   列表中   产品

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!