Feb-25-2019, 01:38 PM
Dear members of python-forum,
I encounter a problem of list definition (i think). I would like to define def(S,T), where:
(i) S and T are two sets (lists in Python) and such that S is a subset of T.
(ii) def(S,T) gives the list of lists K such that S \subset K \subset T. (K is "between" S and T).
For example if S=[2] and T=[2,3,4], i want to get all the lists between [2] and [2,3,4], that is, L=[[2],[2,3],[2,4],[2,3,4]].
In the program below, i define subset(T,S) as being the elements in T which are not in S. In my example, setminus(T,S)=[3,4].
Notice that:
union(A,B) is the union of the lists A and B. (the union of sets).
interzz(A,B) is the intersection of lists A and B (the intersection of sets).
doublonsL removes the lists appearing several times in the list of lists.
The program works as follows: from the infimum S=[2] (for example) and the supremum T=[2,3,4], we compute the elements setminus(T,S) which are in T but not in S. We start from L=[[2]]. The elements 3 and 4 are in T but not in S. We take elements in L and we add lists with these elements setminus(T,S). For example:
[2]
[2],[2,3]
[2],[2,4],[2,3],[2,3,4].
My program, instead of giving what i want, gives Boolean([2],[2,3,4])=
[[2, 3, 4]]. That is, [2], [2,3] and [2,4] are missing.
I guess that my program is ugly but, actually, i just need something that works. Your suggestions would probably be helpful; thank you in advance.
Wishing you a nice week,
Hassediagram.
I encounter a problem of list definition (i think). I would like to define def(S,T), where:
(i) S and T are two sets (lists in Python) and such that S is a subset of T.
(ii) def(S,T) gives the list of lists K such that S \subset K \subset T. (K is "between" S and T).
For example if S=[2] and T=[2,3,4], i want to get all the lists between [2] and [2,3,4], that is, L=[[2],[2,3],[2,4],[2,3,4]].
In the program below, i define subset(T,S) as being the elements in T which are not in S. In my example, setminus(T,S)=[3,4].
Notice that:
union(A,B) is the union of the lists A and B. (the union of sets).
interzz(A,B) is the intersection of lists A and B (the intersection of sets).
doublonsL removes the lists appearing several times in the list of lists.
The program works as follows: from the infimum S=[2] (for example) and the supremum T=[2,3,4], we compute the elements setminus(T,S) which are in T but not in S. We start from L=[[2]]. The elements 3 and 4 are in T but not in S. We take elements in L and we add lists with these elements setminus(T,S). For example:
[2]
[2],[2,3]
[2],[2,4],[2,3],[2,3,4].
My program, instead of giving what i want, gives Boolean([2],[2,3,4])=
[[2, 3, 4]]. That is, [2], [2,3] and [2,4] are missing.
I guess that my program is ugly but, actually, i just need something that works. Your suggestions would probably be helpful; thank you in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
def doublons(LM): # This def is OK. LP = [] for k in LM: if k not in LP: LP.append(k) return (LP) def doublonsL(LM): # This def is OK. LP = [] for listtt in LM: if listtt not in LP: LP.append(listtt) return (LP) def union(A,B): # This def is OK. uni = A for k in B: uni.append(k) return doublons( sorted (uni)) def interzz(lst1, lst2): # This def is OK. lst3 = [value for value in lst1 if value in lst2] return doublons( sorted (lst3)) def setminus(S,T): # This def is OK. for k in T: S.remove(k) return doublons(S) def issubset(S,T): # This def is OK. if interzz(S,T) = = S: return "true" else : return "false" def Boolean(SQ,TQ): # The problem comes from this def. if issubset(SQ,TQ) = = "false" : # We check that S is a subset of T. print ( "error subset" ) ZED = setminus(TQ,SQ) # All the elements of TQ which are not in SQ. LILI = [SQ] MOLI = LILI[:] for k in ZED: for PPP in MOLI: LILI.append(union(PPP,[k])) LILI = MOLI[:] MOLI = LILI[:] for kj in range ( 0 , len (LILI)): LILI[kj] = sorted (doublons(LILI[kj])) return doublonsL(LILI) |
Hassediagram.