Python: Determine if an unsorted list is contained in a 'list of lists', regardless of the order to the elements

前端 未结 4 429
鱼传尺愫
鱼传尺愫 2021-01-29 00:27

I have a similar question to this question: Determine if 2 lists have the same elements, regardless of order?

What is the best/quickest way to determine whether an unsor

4条回答
  •  借酒劲吻你
    2021-01-29 00:52

    Use sets

    def doSomething(myDictOfLists, otherInputs):
    
        list1 = []
        ...  # do something here with `otherInputs' 
        ...  # which gives `list1' some values
    
        # now only append `list1' to `myListOfLists' if it doesn't already exist
        # and if it does exist, remove it
        list1Set = set(list1)
        if list1Set not in myDictOfLists:
            myDictOfLists[list1Set] = list1
    
        return myDictOfLists
    

提交回复
热议问题