On 23/07/2022 05:28, Khairil Sitanggang wrote:
Hello Expert:

I just started using python. Below is a simple code.  I was trying to check
if, say, NO1 is not in the NODELIST[:].NO
How can I achieve this purpose?

Regards,
-Irfan


class Node:
     def __init__(self):
         self.NO = 0
         self.A = 20

NODE = Node()
NODELIST = []

NODE.NO = 10
NODELIST.append(NODE)

NODE.NO = 20
NODELIST.append(NODE)

NODE.NO = 30
NODELIST.append(NODE)


NO1 = 20
if NO1 not in NODELIST[:].NO  ???

No, you can't do it that way. You have to iterate through the list and check each member individually:

    if any(NO1 == N.NO for N in NODELIST):

And another thing: you've created only 1 node, and you're changing it each time before adding it to the list, so the list ends up with 3 references to the _same_ object.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to