Python set to list

后端 未结 7 1917
谎友^
谎友^ 2020-12-22 18:28

How can I convert a set to a list in Python? Using

a = set([\"Blah\", \"Hello\"])
a = list(a)

doesn\'t work. It gives me:

         


        
7条回答
  •  北荒
    北荒 (楼主)
    2020-12-22 19:13

    Your code does work (tested with cpython 2.4, 2.5, 2.6, 2.7, 3.1 and 3.2):

    >>> a = set(["Blah", "Hello"])
    >>> a = list(a) # You probably wrote a = list(a()) here or list = set() above
    >>> a
    ['Blah', 'Hello']
    

    Check that you didn't overwrite list by accident:

    >>> assert list == __builtins__.list
    

提交回复
热议问题