Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A list of lists
#1
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.

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) 
Wishing you a nice week,
Hassediagram.
Reply
#2
python has lists x = [1,2,3]
and sets x = {1,2,3}
plus all set operations see: https://docs.python.org/3.7/library/stdt...-frozenset

aslo see itertools for permutations, combinations and product
Reply
#3
Also, Python has the boolean singletons True and False, so you don't need "true" and "false". If issubset returned those, you could just use if issubset(SQ, TQ):. Of course, since all issubset does is check interzz(), you could just replace that with if interzz(SQ, TQ) == SQ:.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Feb-25-2019, 03:40 PM)Larz60+ Wrote: python has lists x = [1,2,3]
and sets x = {1,2,3}
plus all set operations see: https://docs.python.org/3.7/library/stdt...-frozenset

aslo see itertools for permutations, combinations and product

Thank you Larz60 for your answer; i meant sets in the mathematical sense, which i translate into lists in Python.
Reply
#5
As Larz said, this functionality is all covered in Python. But if you need to recreate it for homework, to get the subsets, start with a list of the empty set. Loop through the items in the set, adding to the list of subsets every subset plus the item:

>>> subsets = [[]]
>>> fullset = [1, 2, 3]
>>> for item in fullset:
...    subsets = subsets + [subset + [item] for subset in subsets]
...
>>> subsets
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
You should be able to modify that to get the list of subsets you want.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Feb-25-2019, 04:14 PM)ichabod801 Wrote: As Larz said, this functionality is all covered in Python. But if you need to recreate it for homework, to get the subsets, start with a list of the empty set. Loop through the items in the set, adding to the list of subsets every subset plus the item:

>>> subsets = [[]]
>>> fullset = [1, 2, 3]
>>> for item in fullset:
...    subsets = subsets + [subset + [item] for subset in subsets]
...
>>> subsets
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
You should be able to modify that to get the list of subsets you want.

Thank you ichabod801, this works ! I will use your trick.
But why didn't it work in my program ?

Thank you also for your suggestion about the intersections.
Reply
#7
(Feb-25-2019, 04:23 PM)Hassediagram Wrote: But why didn't it work in my program ?

I don't know. Your code is really hard to read and very complicated, I just figured there must be a simpler solution. Always keep it simple when programming.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(Feb-25-2019, 04:34 PM)ichabod801 Wrote:
(Feb-25-2019, 04:23 PM)Hassediagram Wrote: But why didn't it work in my program ?

I don't know. Your code is really hard to read and very complicated, I just figured there must be a simpler solution. Always keep it simple when programming.

Yes that is true; ok i got my problem solved, thank you very much ichabod801 !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,014 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,563 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  How to efficiently average same entries of lists in a list xquad 5 2,070 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 1,805 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  behavior list of lists roym 5 2,036 Jul-04-2021, 04:43 PM
Last Post: roym
  List of lists - merge sublists with common elements medatib531 1 3,354 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 10,059 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 2,161 Nov-13-2020, 04:59 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020