What is the Pythonic way to find the longest common prefix of a list of lists?

后端 未结 6 702
温柔的废话
温柔的废话 2020-12-03 07:27

Given: a list of lists, such as [[3,2,1], [3,2,1,4,5], [3,2,1,8,9], [3,2,1,5,7,8,9]]

Todo: Find the longest common

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 08:03

    os.path.commonprefix() works well for lists :)

    >>> x = [[3,2,1], [3,2,1,4,5], [3,2,1,8,9], [3,2,1,5,7,8,9]]
    >>> import os
    >>> os.path.commonprefix(x)
    [3, 2, 1]
    

提交回复
热议问题