我需要验证,如果我的清单列表中有在python同样大小的名单检查,看看是否列出的清单具有相同大小的名单
myList1 = [ [1,1] , [1,1]] // This should pass. It has two lists.. both of length 2
myList2 = [ [1,1,1] , [1,1,1], [1,1,1]] // This should pass, It has three lists.. all of length 3
myList3 = [ [1,1] , [1,1], [1,1]] // This should pass, It has three lists.. all of length 2
myList4 = [ [1,1,] , [1,1,1], [1,1,1]] // This should FAIL. It has three list.. one of which is different that the other
我可以写一个循环来遍历列表,并检查每个子列表的大小。是否有更多的pythonic方式来实现结果。
我在这里看到的问题是(如果编译器没有优化它),你正在计算索引0的列表长度n次加上1次计算每个其他列表,这使得2 * n长度计算。 –
@SanSS,长度不是_calculated_,它是一个属性。如果你确实需要微小的速度提升,你可以将它存储在一个变量中。当列表为空时,您必须特殊处理 –
如果将'all'的参数嵌套到列表中,让这个工作起作用有点麻烦吗? 'all([len(i)== len(myList [0])for myList])' – FriskyGrub