唯一的非子集集列表 [英] list of unique non-subset sets
问题描述
我有很多设置对象,其中一些可以包含同一组对象
而其他对象可以是另一组的子集。给定一个集合列表,
我需要获得一个唯一集合的列表,这样集合中的非集合是另一个集合的
子集,或者包含完全相同的成员。
试图执行以下操作:
s1 = set([''a'',''b'',''c''])
s2 = set(['''',''c''])
s3 = set(['''',''d'', ''e'',''f''])
s4 = set([''r'',''k'',''l''])
s5 = set([''r'',''k'',''l''])
L = [s1,s2,s3,s4,s5]
----------------------->清理清单应包含s1,s3,s5
Lu = []#唯一的清单集
Lu.append(L [0])
我在范围内(1,len(L)):
s = L [i]
#检查是否平等
如果s在L:
继续
#检查子项目
for len in Lu(Lu):
s1 = Lu [j]
如果len(s-s1)== 0:
继续
elif len(s1- s)== 0:
Lu [i] = s1#替换为更大的
但这不起作用因为我得到了
Lu = [set(['''',''c'',''b''])]
我做错了什么?
谢谢
le * ******@yahoo.com 写道:
我有很多设置对象,其中一些可以包含同一组对象
而其他可以是另一个的子集。给定一组集合,
我需要获得一组唯一集合,这样集合中的非集合是另一集合的子集或包含完全相同的成员。
尝试执行以下操作:
s1 = set([''a'',''b'',''c''])
s2 = set([''a'''''' c''])
s3 = set(['''',''d'',''e'',''f''])
s4 = set(['' r'',''k'',''l''])
s5 = set([''r'',''k'',''l''])
L = [s1,s2,s3,s4,s5]
----------------------->清理清单应包含s1,s3,s5
py> s1 = set(['''',''b'',''c''])
py> s2 = set(['''',''c''])
py> s3 = set(['''',''d'',''e'',''f''])
py> s4 = set([''r'',''k'',''l''])
py> s5 = set([''r'',''k'',''l''])
py> lst = [s1,s2,s3,s4,s5]
py> uniqlst = []
py> for lstset in lst:
.... for uniqlstset in uniqlst:
.... if lstset.issubset(uniqlstset):
....破解
....其他:
.... uniqlst.append(lstset)
....
py> uniqlst
[set([''a'',''c'',''b'']),set(['''',''e'',''' d'',''f'']),set([''k'',''r'','l''])]
效率不高, 提个醒。但是我相信它是正确的。
我基本上把你的描述给定了一个集合列表,我需要得到一个
唯一列表设置为[s]的非[e]是
的另一个子集并将其翻译成代码。因此,我在lst中遍历每个元素
,并且只将它添加到uniqlst中,如果它不是
的子集,那么任何已经在uniqlst中的项目。
STeVe
[le ******* @ yahoo.com]我有许多设置对象,其中一些可以包含相同的对象组,而其他对象可以是另一组的子集。给定一组集合,
我需要获得一组唯一集合,这样集合中的非集合是另一集合的子集或包含完全相同的成员。
尝试执行以下操作:
s1 = set([''a'',''b'',''c''])
s2 = set([''a'''''' c''])
s3 = set(['''',''d'',''e'',''f''])
s4 = set(['' r'',''k'',''l''])
s5 = set([''r'',''k'',''l''])
L = [s1,s2,s3,s4,s5]
----------------------->清理清单应该包含s1,s3,s5
这应该可以解决问题:
result = []
$对于s1中的L
:
结果为s2的
:
如果s1< = s2:
$
否则:
result.append(s1)
打印结果
Raymond Hettinger
Raymond Hettinger写道:[le*******@yahoo.com]
我有很多套其中一些对象可以包含相同的对象组,而其他对象可以是另一对象的子集。给定一组集合,
我需要获得一组唯一集合,这样集合中的非集合是另一集合的子集或包含完全相同的成员。
尝试执行以下操作:
s1 = set([''a'',''b'',''c''])
s2 = set([''a'''''' c''])
s3 = set(['''',''d'',''e'',''f''])
s4 = set(['' r'',''k'',''l''])
s5 = set([''r'',''k'',''l''])
L = [s1,s2,s3,s4,s5]
----------------------->清理后的清单应该包含s1,s3,s5
这应该可以解决这个问题:
结果= []
对于L中的s1:
对于s2的结果:
如果s1< = s2:
打破
否则:
result.append(s1)
打印结果
如果我正确理解原始帖子,你还需要检查现有的集合是否是你所拥有的集合的b $ b子集加入。一个更好的测试用例是
s1 = set(['''',''b'',''c''])
s2 = set([ ''a'',''c''])
s3 = set(['''',''d'',''e'',''f''] )
s4 = set([''r'',''k'',''l''])
s5 = set([''r' ',''k'',''l''])
s6 = set([''g'',''h''])
s7 = set([''h'',''i''])
s8 = set([''g'',''h'',''i''])
L = [s1,s2,s3,s4,s5,s6,s7,s8]
#----------- ------------>清理清单应该包含s1,s3,s5,s8
雷蒙德和STeVe的建议都失败了。
Kent
Hi,
I have many set objects some of which can contain same group of object
while others can be subset of the other. Given a list of sets,
I need to get a list of unique sets such that non of the set is an
subset of another or contain exactly the same members.
Tried to do the following:
s1=set([''a'',''b'',''c''])
s2=set([''a'',''c''])
s3=set([''a'',''d'',''e'',''f''])
s4=set([''r'',''k'',''l''])
s5=set([''r'',''k'',''l''])
L=[s1,s2,s3,s4,s5]
----------------------- > cleaned-up list should contain s1, s3, s5
Lu=[] # unique list of sets
Lu.append( L[0] )
for i in range(1,len(L)):
s=L[i]
# check for equality
if s in L:
continue
# check for subseting
for j in len(Lu):
s1=Lu[j]
if len (s-s1)==0:
continue
elif len(s1-s)==0:
Lu[i]=s1 # replace with the bigger
But this does not work since I get
Lu=[set([''a'', ''c'', ''b''])]
What am I doing wrong?
thanks
le*******@yahoo.com wrote:Hi,
I have many set objects some of which can contain same group of object
while others can be subset of the other. Given a list of sets,
I need to get a list of unique sets such that non of the set is an
subset of another or contain exactly the same members.
Tried to do the following:
s1=set([''a'',''b'',''c''])
s2=set([''a'',''c''])
s3=set([''a'',''d'',''e'',''f''])
s4=set([''r'',''k'',''l''])
s5=set([''r'',''k'',''l''])
L=[s1,s2,s3,s4,s5]
----------------------- > cleaned-up list should contain s1, s3, s5
py> s1 = set([''a'',''b'',''c''])
py> s2 = set([''a'',''c''])
py> s3 = set([''a'',''d'',''e'',''f''])
py> s4 = set([''r'',''k'',''l''])
py> s5 = set([''r'',''k'',''l''])
py> lst = [s1, s2, s3, s4, s5]
py> uniqlst = []
py> for lstset in lst:
.... for uniqlstset in uniqlst:
.... if lstset.issubset(uniqlstset):
.... break
.... else:
.... uniqlst.append(lstset)
....
py> uniqlst
[set([''a'', ''c'', ''b'']), set([''a'', ''e'', ''d'', ''f'']), set([''k'', ''r'', ''l''])]
Not horribly efficient, mind you. But I believe it''s correct.
I basically took your description "Given a list of sets, I need to get a
list of unique sets such that non[e] of the set[s] is an subset of
another" and translated that to code. So I iterate through each element
in lst, and and only add that element to uniqlst if it''s not a subset of
any of the items already in uniqlst.
STeVe
[le*******@yahoo.com]I have many set objects some of which can contain same group of object
while others can be subset of the other. Given a list of sets,
I need to get a list of unique sets such that non of the set is an
subset of another or contain exactly the same members.
Tried to do the following:
s1=set([''a'',''b'',''c''])
s2=set([''a'',''c''])
s3=set([''a'',''d'',''e'',''f''])
s4=set([''r'',''k'',''l''])
s5=set([''r'',''k'',''l''])
L=[s1,s2,s3,s4,s5]
----------------------- > cleaned-up list should contain s1, s3, s5
This should do the trick:
result = []
for s1 in L:
for s2 in result:
if s1 <= s2:
break
else:
result.append(s1)
print result
Raymond Hettinger
Raymond Hettinger wrote:[le*******@yahoo.com]I have many set objects some of which can contain same group of object
while others can be subset of the other. Given a list of sets,
I need to get a list of unique sets such that non of the set is an
subset of another or contain exactly the same members.
Tried to do the following:
s1=set([''a'',''b'',''c''])
s2=set([''a'',''c''])
s3=set([''a'',''d'',''e'',''f''])
s4=set([''r'',''k'',''l''])
s5=set([''r'',''k'',''l''])
L=[s1,s2,s3,s4,s5]
----------------------- > cleaned-up list should contain s1, s3, s5
This should do the trick:
result = []
for s1 in L:
for s2 in result:
if s1 <= s2:
break
else:
result.append(s1)
print result
If I understand the original post correctly, you also need to check for an existing set being a
subset of the set you are adding. A better test case is
s1=set([''a'',''b'',''c''])
s2=set([''a'',''c''])
s3=set([''a'',''d'',''e'',''f''])
s4=set([''r'',''k'',''l''])
s5=set([''r'',''k'',''l''])
s6=set([''g'', ''h''])
s7=set([''h'', ''i''])
s8=set([''g'', ''h'', ''i''])
L=[s1,s2,s3,s4,s5,s6,s7,s8]
# ----------------------- > cleaned-up list should contain s1, s3, s5, s8
which both Raymond and STeVe''s proposals fail.
Kent
这篇关于唯一的非子集集列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!