Python - can't import Set from sets (“no module named sets”)

前端 未结 3 1023
北海茫月
北海茫月 2021-01-03 19:46

I\'m trying to learn python and I was trying to write something simple. I am developing under Pydev (Eclipse) using OS X 10.8. I installed python 3.2 using the 64bits .dmg i

相关标签:
3条回答
  • 2021-01-03 20:22

    You don't need the sets module anymore. set is a built-in class in Python 3 and can be used without import.

    my_set = set()
    
    0 讨论(0)
  • 2021-01-03 20:39

    you don't need to use

    from sets import Set
    engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
    

    above is Deprecated since version 2.6:

    you can use below code above 2.6 version

    engineers = set(['John', 'Jane', 'Jack', 'Janice'])
    programmers = set(['Jack', 'Sam', 'Susan', 'Janice'])
    managers = set(['Jane', 'Jack', 'Susan', 'Zack'])
    employees = engineers | programmers | managers  
    print(employees)
    
    0 讨论(0)
  • 2021-01-03 20:40

    In every recent python version sets are builtin as set and Python 3 got rid of the deprecated sets module altogether.

    If you wanted to ensure that the code also works with ancient versions you could do something like this though:

    try:
        set
    except NameError:
        from sets import Set as set
    

    If you need to run old code and don't want to change it (bad!):

    try:
        from sets import Set
    except ImportError:
        Set = set
    
    0 讨论(0)
提交回复
热议问题