Elegant way to compare sequences

前端 未结 8 2013
死守一世寂寞
死守一世寂寞 2021-02-05 20:10

Does python provide an elegant way to check for \"equality\" of sequences of different types? The following work, but they seem rather ugly and verbose for python code:

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 20:43

    Convert both sequences to lists, and use builtin list comparison. It should be sufficient, unless your sequences are really large.

    list(a) == list(b)
    

    Edit:

    Testing done by schickb shows that using tuples is slightly faster:

    tuple(a) == tuple(b)
    

提交回复
热议问题