Checking to see if a list of lists has equal sized lists

前端 未结 4 888
梦谈多话
梦谈多话 2021-01-12 12:32

I need to validate if my list of list has equally sized lists in python

myList1 = [ [1,1] , [1,1]] // This should pass. It has two lists.. both of length 2
m         


        
4条回答
  •  不知归路
    2021-01-12 13:06

    def equalSizes(*args):
        """
        # This should pass. It has two lists.. both of length 2
        >>> equalSizes([1,1] , [1,1])
        True
    
        # This should pass, It has three lists.. all of length 3
        >>> equalSizes([1,1,1] , [1,1,1], [1,1,1])
        True
    
        # This should pass, It has three lists.. all of length 2
        >>> equalSizes([1,1] , [1,1], [1,1])
        True
    
        # This should FAIL. It has three list.. one of which is different that the other
        >>> equalSizes([1,1,] , [1,1,1], [1,1,1])
        False
        """
        len0 = len(args[0])
        return all(len(x) == len0 for x in args[1:])
    

    To test it save it to a file so.py and run it like this:

    $ python -m doctest so.py -v
    Trying:
        equalSizes([1,1] , [1,1])
    Expecting:
        True
    ok
    Trying:
        equalSizes([1,1,1] , [1,1,1], [1,1,1])
    Expecting:
        True
    ok
    Trying:
        equalSizes([1,1] , [1,1], [1,1])
    Expecting:
        True
    ok
    Trying:
        equalSizes([1,1,] , [1,1,1], [1,1,1])
    Expecting:
        False
    ok
    

提交回复
热议问题