Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automatic redefining
#1
Dear Python-forum members,

I have registered to this forum because i have a small but resistant problem.
It is surely trivial, but i have not practiced Python for a long time and i don't remember the trick, and i would be grateful to benefit from someone's help.

(This def is to produce neighbors in a Hasse diagram).

Let "Collection" be a collection of vectors, initially empty.

From an initial vector that i call : "SET", i will produce other vectors that i will add to "Collection". Then i return "Collection".

To do this, I will add or remove elements of SET, and i add these vectors to "Collection".

My problem is that i don't want "SET" to change. When i write C=SET, i want to modify C, but i don't want "SET" to be modified when i modify C. However with this code, "SET" is modified when i modify C. (I can see the change with : print("SET before",SET); and : print("SET after",SET))

def PHa(SET):
    Collection=[]
    for k in range(1,n+1):
        if k not in SET:
            C=SET
            print("SET before",SET)
            C.append(k)
            print("SET after",SET)
            Collection.append(C)
    for k in range(1,n+1):
        if k in SET:
            C=SET
            C.remove(k)
            Collection.append(C)
    return Collection
Does someone know the trick to fix "SET" ?

Thank you very much in advance,
Hassediagram
Reply
#2
I suppose SET is a list. You should provide more details.
List is a mutable data type so changes to a list variable will remain. And assigning C to SET will point to same variable.
To copy the contents of SET to new variable C, you can do:
C=SET[:]
Reply
#3
n is not defined.

You probably want something like:
def PHa(SET, n):
or to define 'n' as a global outside the function (not recommended) and leave the 'PHa' line as is.

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#4
Dear Lewis, Yes I have defined n globally in the code before. Thank you for your remark.

Dear j.crater, Great ! It works. Thank you very much for your help; problem solved !
Reply
#5
Dear members of python-forum,

I have another problem. I think it is linked to the one i had last year (above).

I want to define a 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
#6
Message deleted; i am opening a new topic.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 275 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  Matplotlib - automatic update frohr 1 1,078 Mar-29-2022, 07:05 PM
Last Post: deanhystad
  automatic create folders Mr_Kool 4 1,717 Dec-21-2021, 04:38 PM
Last Post: BashBedlam
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,727 May-18-2021, 06:42 PM
Last Post: Skaperen
  Automatic registering python to registry kozaizsvemira 1 2,142 Oct-22-2019, 11:23 AM
Last Post: kozaizsvemira
  Idea of timelapse with automatic settings hhanswurster 2 3,193 May-26-2018, 01:43 PM
Last Post: killerrex
  python to install printer drivers automatic leoxucn 4 7,588 Jul-10-2017, 09:21 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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