2016-05-01 14 views
1

Es scheint, Sets sind in Python 2.7 veraltet und fragen sich, was ist die Alternative für ungeordnete eindeutige Sammlung? Vielen Dank.Sets in Python 2.7 veraltet

from sets import Set 

a = Set() 
a.add("1") 
a.add("2") 
a.add("3") 

if "1" in a: 
    print "1" 
if "Hello" in a: 
    print "Hello" 

Grüßen, Lin

+1

Verwenden Sie einfach ein Set(). –

Antwort

6

Sets sind noch in Python 2.7 verfügbar und werden gebaut in

Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> a_set = set([1, 2, 3]) 
>>> a_set 
set([1, 2, 3]) 
>>> b_set = {1, 2, 3} 
>>> b_set 
set([1, 2, 3]) 

Docs. https://docs.python.org/2/library/sets.html Und die Hinweise auf der oben auf der Seite sehen:

Veraltet seit Version 2.6: Das integrierte Set/Frozenset t ypes ersetzen dieses Modul.

+1

Dank tknickman, markieren Sie Ihre Antwort als Antwort. Funktioniert bei mir. :) –