List comprehension vs set comprehension

前端 未结 2 844
梦谈多话
梦谈多话 2021-01-18 01:39

I have the following program. I am trying to understand list comprehension and set comprehension:

mylist = [i for i in range(1,10)]
print(mylist)

clist = []         


        
2条回答
  •  耶瑟儿~
    2021-01-18 02:18

    Curly braces are used for both dictionary and set comprehensions. Which one is created depends on whether you supply the associated value or not, like following (3.4):

    >>> a={x for x in range(3)}
    >>> a
    {0, 1, 2}
    >>> type(a)
    
    >>> a={x: x for x in range(3)}
    >>> a
    {0: 0, 1: 1, 2: 2}
    >>> type(a)
    
    

提交回复
热议问题