0

I have the following code:

receps =[1,1,1,2,2,2,2,3,3,3,3,3,3]
listRecep = []
listNumRecep = []
class Line:
    def __init__(self,recep) :
        self.recep = recep
        
class Recep:
    recep=""
    lines = []

for recep in receps:
        l=Line(recep)
        if recep not in listNumRecep:
            listNumRecep.append(recep)
            r=Recep()
            r.recep=recep
            r.lines.append(l)
            #print(l.recep)
            listRecep.append(r)
        else:
            for re in listRecep:
                if re.recep == recep:
                    re.lines.append(l)
                    #print(l.recep)
        
for re in listRecep:
    print (re.lines[0].recep)

Basically what i want is to store every lines "Line" of the same "Recep" into a list contained in a "Recept" object. Then i print the recep of the first line.

So the output expected would be:

1
2
3

but actually i have

1
1
1

It looks like the first line is stored every time in the list and that's what I don't understand because when i print l.recep(commented lines) the correct output is printed so the problem is when the "Line" is affected to the "Recep".

4
  • 1
    Think carefully about why your code includes a line that looks like self.recep = recep, but does not include a line that looks like self.lines = []. Commented Jun 14, 2021 at 6:33
  • Also, please think about variable naming. When reading code, one would assume recep would be an instance of Recep, but it is an integer in one place, and a string in another; one would assume re would be the regexp package, but it isn't; l and r are understandable in a short snippet but would be hell in a longer code (also, one might think they stand for "left" and "right", when used close together). None of this is an error, as such, but makes errors very easy, and debugging hard. Commented Jun 14, 2021 at 6:37
  • Thank you for you answers this code is just a snippet example I made from the original code for the post but I still don't understand why the first line is stored in all recep objects Commented Jun 14, 2021 at 6:58
  • Ok I figured it out thank you very much Commented Jun 14, 2021 at 7:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.