Comparing the order of specific elements in a python list -
how implement following comparison using python 2
input composed of 2 groups:
- different lists collected experiments.
- all accepted sequences of of these elements.
how filter lists input group 1. of accepted sequences input group 2 proper subsequence?
for example:
group 2 (defined user):
x = [3, 1, 6] y = [2, 1, 6] z = [3, 4, 6]
group 1 (from experiments):
a = [1, 2, 3, 5, 6, 7]
b = [2, 1, 4, 3, 1, 8, 6]
c = [6, 3, 5, 7, 8, 4, 2, 6]
d = [1, 2, 1, 3, 4]
we accept b
, c
because x
subsequence of b
, z
subsequence of c
. , likewise reject a
, d
because none of x
, y
or z
subsequence of either.
mysterious(a) should return [2,6] not acceptable didn't visit node 1 after 2
mysterious(b) should return [2,1,6] acceptable , on
another example(detailed):
to accept set or list need elements present services.
servicea served [ 3 , 2 ] serviceb served [ 1 , 4 ] servicec served [ 6 ]
total nodes available end user [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
we ask him choose set or list total nodes. accept combination when nodes serve services appear in correct order or sequence.
so user choose set unlimited number of nodes long as: 1. nodes member total nodes. 2. order of services correct.
example [1,4,5,"node serve a", 7,1,2, "node serve b", "node serve c"]
or general form accept list or set is: [some elements, element serve service a, other elements, element service b, more elements, element service c, etc...]
and can replace node service element element correspondent set above * if not clear please let me know , explain in more examples.
example three:
lets think in factory 10 machines. product need 3 different processes manufactured.
every machine can of these processes or differs. machine 1 can process alpha, gama not beta. machine 2 can process alpha
every raw material arrives need find route through machines condition end should have 3 processes done.
the processes must in order first alpha, beta, @ end gama. route every time avoid overloading machines.
so need function accept or reject route suggestions enforce every raw material go through processes in correct order.
i can't of course make possible combinations , compare consume time , can run infinity.
thanks
i wrote following code longest_subsequence
a = [1, 2, 3, 5, 6, 7] b = [2, 1, 4, 3, 1, 8, 6] c = [6, 3, 5, 7, 8, 4, 2, 6] d = [1, 2, 1, 3, 4] x = [3, 1, 6] y = [2, 1, 6] z = [3, 4, 6] group1 = [a, b, c, d] group2 = [x, y, z] def longest_subsequence(experiment): longest = [] accepted_sequence in group2: = iter(experiment) subsequence = [] element in accepted_sequence: if element in it: subsequence.append(element) else: break if subsequence == list(accepted_sequence): return subsequence longest = max(longest, subsequence, key=len) return longest experiment in group1: print(longest_subsequence(experiment))
it works using element in iterator
, while scanning next matching element, discards other elements in between.
the code finds first longest subsequence in group2
. since x
precedes y
, both of them subsequences of b
, x
printed b
:
[3] [3, 1, 6] [3, 4, 6] [2, 1]
Comments
Post a Comment