Python set to list

后端 未结 7 1898
谎友^
谎友^ 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条回答
  •  梦毁少年i
    2020-12-22 19:13

    This will work:

    >>> t = [1,1,2,2,3,3,4,5]
    >>> print list(set(t))
    [1,2,3,4,5]
    

    However, if you have used "list" or "set" as a variable name you will get the:

    TypeError: 'set' object is not callable
    

    eg:

    >>> set = [1,1,2,2,3,3,4,5]
    >>> print list(set(set))
    Traceback (most recent call last):
    File "", line 1, in 
    TypeError: 'list' object is not callable
    

    Same error will occur if you have used "list" as a variable name.

提交回复
热议问题