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


Messages In This Thread
Automatic redefining - by Hassediagram - May-12-2018, 04:19 PM
RE: Automatic redefining - by j.crater - May-12-2018, 04:34 PM
RE: Automatic redefining - by ljmetzger - May-12-2018, 04:40 PM
RE: Automatic redefining - by Hassediagram - May-12-2018, 04:43 PM
RE: Automatic redefining - by Hassediagram - Feb-25-2019, 09:39 AM
RE: Automatic redefining - by Hassediagram - Feb-25-2019, 01:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 377 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  Matplotlib - automatic update frohr 1 1,134 Mar-29-2022, 07:05 PM
Last Post: deanhystad
  automatic create folders Mr_Kool 4 1,804 Dec-21-2021, 04:38 PM
Last Post: BashBedlam
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,833 May-18-2021, 06:42 PM
Last Post: Skaperen
  Automatic registering python to registry kozaizsvemira 1 2,225 Oct-22-2019, 11:23 AM
Last Post: kozaizsvemira
  Idea of timelapse with automatic settings hhanswurster 2 3,270 May-26-2018, 01:43 PM
Last Post: killerrex
  python to install printer drivers automatic leoxucn 4 7,735 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