且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查列表中是否有两个项目按特定顺序排列?

更新时间:2023-11-28 23:13:04

v = [1,2,3,4,3,1,2]
any([2,3] == v[i:i+2] for i in xrange(len(v) - 1))

虽然@PaoloCapriotti的版本可以解决问题,但此方法更快,因为一旦找到匹配项,它将停止解析v.

While @PaoloCapriotti's version does the trick, this one is faster, because it stops parsing the v as soon as a match is found.