I have the following list:
I=[1,4, 3, 7,8,9]
I have another two lists which are subsets of list I
:
IU=[3,9]
IUU=[4,8]
I have to merge lists IU
and IUU
to create list J
in such a way that the elements of the merged list follow the orders of the elements of list I
.
J=[4,3,8,9]
I am looking for some ideas on how to implement this. If I just add the sublist I get following result.
J=IU IUU
J=[3, 9, 4, 8]
I will appreciate your comments/answers.
CodePudding user response:
You can just do something like:
sorted(IU IUU, key={e:i for i,e in enumerate(I)}.get)
Which should be O(N*Log N)
The problem with key=I.index
is that this will force your performance to deteriorate to O(N**2).
CodePudding user response:
I found the answer. Thanks for looking.
sorted(J,key=I.index)