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

后端 未结 6 703
温柔的废话
温柔的废话 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条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 08:21

    It is inefficient as it doesn't early-out as soon as a mismatch is found, but its tidy:

    ([i for i,(j,k) in enumerate(zip(a,b)) if j!=k] or [0])[0]
    

提交回复
热议问题