List comprehension vs set comprehension

前端 未结 2 826
梦谈多话
梦谈多话 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:24

    Set is an unordered, mutable collection of unrepeated elements.

    In python you can use set() to build a set, for example:

    set>>> set([1,1,2,3,3])
    set([1, 2, 3])
    >>> set([3,3,2,5,5])
    set([2, 3, 5])
    

    Or use a set comprehension, like a list comprehension but with curly braces:

    >>> {x for x in [1,1,5,5,3,3]}
    set([1, 3, 5])
    

提交回复
热议问题