python - Specific sequence stored in a list -
i looking way efficiently search lists have particular sequence of values. sequence important! example:
[x,y,z] , [x,z,y] contain same values sequence different
however:
- [x,y,z], [y,z,x] , [z,x,y] same me.
- [x,z,y], [z,y,x] , [x,z,y] same too.
i think bout running script parts of connections. example, if looking [x,y,z] look
mylist1 = ['a','b','c'] mylist2 = ['b','a','c'] def is_sequence_same(thelist,somelist): if (thelist[0] == somelist[0] , thelist[1] == somelist[1]): return true if (thelist[1] == somelist[1] , thelist[2] == somelist[2]): return true if (thelist[0] == somelist[1] , thelist[1] == somelist[0]): return false if (thelist[0] == somelist[2] , thelist[1] == somelist[2]): return false else: return none is_sequence_same(mylist1,mylist2)
function returns: true - if sequence same have asked, false - if sequence opposite
my current function incomplete. however, think there should more elegant ways of solving problem
use deque:
from collections import deque def is_sequence_same(l1, l2): if l1 == l2: return true if set(l1) != set(l2) or len(l1) != len(l2): return false d2 = deque(l2) in range(len(l2)): if l1 == list(d2): return true d2.rotate() return false
Comments
Post a Comment