集的笛卡尔积,其中在Python的排列下没有元素相同 [英] Cartesian Product of Sets where No Elements are Identical under Permutations in Python

查看:84
本文介绍了集的笛卡尔积,其中在Python的排列下没有元素相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我想采用的笛卡尔积,效果很好.但是,我要删除此新集合中所有在元素置换后都相同的元素.

I have some sets I would like to take the Cartesian product of, which is working well. However, I want to remove all elements of this new set which are identical under a permutation of the elements.

例如,采用以下代码:

import itertools as ittools
x = 2
y = 3
z = 5

flist = list(ittools.product([x,y,z],repeat=3))

for f in flist:
    print reduce(lambda a,b: a*b, f)

此代码找到集合{2,3,5}的笛卡尔积,并返回结果集中每个元素的所有三个分量的乘积.但是,某些数字会出现多次,即12可写为2 * 2 * 3、2 * 3 * 2或3 * 2 * 2.我想删除这些重复项中的所有(除了一个)实例.

This code find the Cartesian product of the set {2,3,5} and returns the product of all three components of each element in the resulting set. However, some numbers appear multiple times, i.e. 12 can be written as 2*2*3, 2*3*2, or 3*2*2. I would like to remove all but one instance of these duplicates.

我知道这从根本上讲是一个组合问题,但这似乎是Python中可能有一个不错的解决方案,它不涉及对列表进行额外的传递,就像我在这里为笛卡尔笛卡尔的每个元素计算一些标识符一样产品.

I know that this fundamentally a combinatorics problem, but this seems like there is probably a nice solution in Python that doesn't involve doing an extra pass over the list like I did here to compute some identifier for each element of the Cartesian product.

推荐答案

您要 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆