有一些像这样的帖子 I have a list of numbers, how to generate all unique k-partitions?
但是我想知道是否有一些新的有效的库来解决这个问题(itertools
)?sagemath
?)在
我有一个数字列表,如何生成所有唯一的有序k分区?
例如,如果我有[1,2,3,4,5]
并且k=3
[[1,2],[3],[4,5]]
就是这样一个分区
但是[[4,5],[3],[1,2]]
也是这样一个分区
例如,我还想将空集作为k子集中的一个可能集
[[2,3],[],[1,4,5]]
顺序关系到
^{2}$和[[4,5],[3],[1,2]]
但是如果你跟我来的话,[[2,1],[3],[5,4]]就和[[1,2],[3],[4,5]]是一样的。。。在
据我所知,来自Sagemath的OrderedSetPartitions(5,3)
不会为我的问题提供答案,因为它排除了空集
编辑:这里有一个(完全没有优化的)尝试,用SAGEMATH天真地解决这个问题
def OrderedSetPartitions_0(A,k):
cols={i for i in range(k)}
# returns the list of k-OrderedSetPartitions of A, allowing for the empty set
s=Subsets(cols).list()
res=[]
count=0
P=[OrderedSetPartitions(A,i) for i in range(k+1)]
for sub in s:
print("sub=")
print(sub)
tmp=[ {} for i in range(k)]
c=sub.cardinality()
for part in P[c]:
print("part=")
print(part)
for i in range(c):
tmp[sub[i]]=part[i]
print("tmp=")
print(tmp)
res=res.append([tmp])
# res = res.append(tmp) # tried this too
print("res=")
print(res)
count=count+1
return(res)
# print(count)
A=range(3)
k=2
A
P=[OrderedSetPartitions(A,i) for i in range(k+1)]
# note that P[2].list is a list of list !
P[2].list()
[[{0, 1}, {2}], [{0, 2}, {1}], [{1, 2}, {0}], [{0}, {1, 2}], [{1}, {0, 2}], [{2}, {0, 1}]]
myset=OrderedSetPartitions_0(A,k)
我收到了这个错误信息,我承认我一点也不明白,因为编码的时候看起来很好, 但不知何故res似乎是“None”而不是[]
sub= {} sub= {0} part= [{0, 1, 2}] tmp= [{0, 1, 2}, {}] res= None sub= {1} part= [{0, 1, 2}] tmp= [{}, {0, 1, 2}] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_21.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("#
-- coding: utf-8 --\n" + support.preparse_worksheet_cell(base64.b64decode("bXlzZXQ9T3JkZXJlZFNldFBhcnRpdGlvbnNfMChBLGsp"),globals())+"\n"); execfile(os.path.abspath("code.py")) File "", line 1, in
File "/private/var/folders/gm/z065gk616xg6g0xgn4c7_bvc0000gn/T/tmpryfYOj/___code___.py", line 2, in <module> exec compile(u'myset=OrderedSetPartitions_0(A,k) File "", line 1, in <module> File "/private/var/folders/gm/z065gk616xg6g0xgn4c7_bvc0000gn/T/tmpSH_9LF/___code___.py", line 27, in OrderedSetPartitions_0 res=res.append([tmp]) AttributeError: 'NoneType' object has no attribute 'append'
问题是如何将列表聚合成res。如果我对所有涉及res的行加一个sharp,我就可以正确地枚举输出
编辑: 谢谢你的回答
实际上我把res=res.append(tmp)
改成了res.append(tmp)
我在执行print(tmp)
时正确地进行了枚举
[{0, 1, 2}, {}, {}] [{}, {0, 1, 2}, {}] [{}, {}, {0, 1, 2}] [{0, 1}, {2}, {}] [{0, 2}, {1}, {}] [{1, 2}, {0}, {}] [{0}, {1, 2}, {}] [{1}, {0, 2}, {}] [{2}, {0, 1}, {}] [{0, 1}, {}, {2}] [{0, 2}, {}, {1}] [{1, 2}, {}, {0}] [{0}, {}, {1, 2}] [{1}, {}, {0, 2}] [{2}, {}, {0, 1}] [{}, {0, 1}, {2}] [{}, {0, 2}, {1}] [{}, {1, 2}, {0}] [{}, {0}, {1, 2}] [{}, {1}, {0, 2}] [{}, {2}, {0, 1}] [{0}, {1}, {2}] [{0}, {2}, {1}] [{1}, {0}, {2}] [{2}, {0}, {1}] [{1}, {2}, {0}] [{2}, {1}, {0}]
但奇怪的是res是错的,肯定有一些副作用
[[{0, 1, 2}, {}, {}],
[{}, {0, 1, 2}, {}],
[{}, {}, {0, 1, 2}],
[{2}, {0, 1}, {}],
[{2}, {0, 1}, {}],
[{2}, {0, 1}, {}],
[{2}, {0, 1}, {}],
[{2}, {0, 1}, {}],
[{2}, {0, 1}, {}],
[{2}, {}, {0, 1}],
[{2}, {}, {0, 1}],
[{2}, {}, {0, 1}],
[{2}, {}, {0, 1}],
[{2}, {}, {0, 1}],
[{2}, {}, {0, 1}],
[{}, {2}, {0, 1}],
[{}, {2}, {0, 1}],
[{}, {2}, {0, 1}],
[{}, {2}, {0, 1}],
[{}, {2}, {0, 1}],
[{}, {2}, {0, 1}],
[{2}, {1}, {0}],
[{2}, {1}, {0}],
[{2}, {1}, {0}],
[{2}, {1}, {0}],
[{2}, {1}, {0}],
[{2}, {1}, {0}]]
前3行是正确的,然后它开始偏离我得到的print(tmp)
。这对我来说很奇怪,因为print(tmp)
和{
这是一个Sagemath中的解决方案,使用NumPy数组和itertools。其思想与代码中的相同:创建OrderedSetPartitions并用空集充实它们。为了在没有太多循环的情况下做到这一点,使用了NumPy数组:关键部分是
partitions[:, s] = P
,其中2D数组partitions
的某些列最初填充了空集,然后被来自OrderedSetPartitions的非空集替换。在输出是一个双NumPy数组。如果需要Python列表,可以返回
all_partitions.tolist()
。在技术性
Sage集(用
Set([1,2,3])
创建)和Python集(用set([1,2,3])
或{1,2,3,4,5}
创建)是不同类的对象。在Sagemath中,Sage集的输出看起来更好:它们显示为{1,2,3},而Python集显示为set([1,2,3])
。因此,Sagemath中最好使用Sage集。另外,OrderedSetPartitions返回Sage集。在但是让NumPy使用Sage集需要更多的精力:特别是,我不能让
np.full
接受空的Sage setSet()
作为填充对象。这就是使用np.empty
并填充它的原因。在一个类似的问题导致了这种情况
i == 1
被不同地处理:NumPy试图将[[Set([1,2,3,4,5])]]
转换为三维数字数组,而不是包含一个Sage set对象的二维数组。在相关问题 更多 >
编程相关推荐