吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1231|回复: 17
收起左侧

[学习记录] python遍历法、迭代法求多个集合的笛卡尔积

  [复制链接]
mtDNA 发表于 2022-11-29 00:27
关于求笛卡尔积,我个人想到了两种方法

遍历法:

def cartesian_product(*args, merge=False):  #*args包括两个及两个以上集合,merge参数为Tue则结果的序偶合并为多元有序组
    for n in range(len(args)-1):
        Product=set()
        S1, S2=args[:2]                     #对于多次求笛卡尔积,每一次取前两个集合参与运算
        if merge==True:
            for i in S1:
                x=(i,) if type(i)!=tuple else i
                for j in S2:
                    y=(j,) if type(j)!=tuple else j
                    Product.add(x+y)
        else:
            for i in S1:
                for j in S2:
                    Product.add((i, j))
        args=(Product,)+args[2:]            #每次运算结束,后边的不动,将前两个集合参数替换为结果,继续参与下一次运算

    return Product



迭代法:
def cartesian_product(*args, merge=False):
    Product=set()
    S1, S2=args[:2]
    if merge==True:
        for i in S1:
            x=(i,) if type(i)!=tuple else i
            for j in S2:
                y=(j,) if type(j)!=tuple else j
                Product.add(x+y)
    else:
        for i in S1:
            for j in S2:
                Product.add((i, j))
    args=(Product,)+args[2:]

    if len(args)==1:        #只剩一个参数时即为结果
        return Product
    else:
        return cartesian_product(*args, merge=merge)



调试(两种方法的都一样):
>>> s1={1, 2, 3}
>>> s2={True, False}
>>> s3={"a", "b", "c", "d"}
>>> cartesian_product(s1, s2, s3, merge=False)
{((2, False), 'd'), ((1, False), 'a'), ((3, False), 'd'), ((1, False), 'b'), ((2, True), 'a'), ((2, False), 'c'), ((1, True), 'c'), ((2, True), 'b'), ((1, True), 'd'), ((1, True), 'b'), ((1, True), 'a'), ((1, False), 'c'), ((3, True), 'c'), ((3, False), 'a'), ((2, True), 'c'), ((2, True), 'd'), ((2, False), 'b'), ((3, False), 'b'), ((2, False), 'a'), ((1, False), 'd'), ((3, False), 'c'), ((3, True), 'b'), ((3, True), 'd'), ((3, True), 'a')}
>>> cartesian_product(s1, s2, s3, merge=True)
{(1, True, 'd'), (1, False, 'd'), (3, True, 'a'), (3, False, 'b'), (3, True, 'd'), (1, False, 'a'), (1, False, 'b'), (3, False, 'd'), (3, True, 'b'), (2, False, 'a'), (3, False, 'a'), (2, False, 'd'), (2, True, 'd'), (2, False, 'b'), (2, True, 'c'), (1, True, 'a'), (2, False, 'c'), (1, True, 'b'), (1, False, 'c'), (3, True, 'c'), (3, False, 'c'), (1, True, 'c'), (2, True, 'a'), (2, True, 'b')}
>>> 

免费评分

参与人数 5吾爱币 +4 热心值 +4 收起 理由
XXZZ8835 + 1 + 1 不明觉厉
paypojie + 1 帮顶 自己也是py相关的
qianfantian + 1 我很赞同!
yyb414 + 1 + 1 热心回复!
为之奈何? + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

aceronethree 发表于 2022-11-29 02:27
看不懂,但是后面总会学到
andInn 发表于 2022-11-29 08:40
hs248613 发表于 2022-11-29 08:43
大白baymax 发表于 2022-11-29 08:53
现在在学习python,看下楼主代码参考一下
byebye518 发表于 2022-11-29 08:57
这个厉害呀。 要多学习
qitianshun 发表于 2022-11-29 09:08
我只会普通遍历
qianseshitou 发表于 2022-11-29 09:21
笛卡尔积不明觉厉
Agolong 发表于 2022-11-29 09:22
学习学习
emmaus7777 发表于 2022-11-29 09:23
谢谢大佬分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-23 17:27

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表