“add to set” returns a boolean in java - what about python?

前端 未结 5 736
小蘑菇
小蘑菇 2021-01-11 17:24

In Java I like to use the Boolean value returned by an \"add to the set\" operation to test whether the element was already present in the set:

if (set.add(\         


        
5条回答
  •  臣服心动
    2021-01-11 18:17

    I guess, in Python is basically add y to your set if needed and doesn't return anything. You can test if y is already in z if you would like to know if the add will change anything.

    Here some tests you can do in iPython :

    In [2]: z = set()
    
    In [3]: y = 4
    
    In [4]: z.add(y)
    
    In [5]: t = z.add(y)
    
    In [6]: t
    
    In [7]: print t
    None
    

提交回复
热议问题