TransWikia.com

selective deletions from list

Mathematica Asked by Suite401 on April 23, 2021

I have a list:

lis = {{"b","x","d"},{"a","z","b"},{"a","x","b"},{"a","x","c"},{"b","z","d"}}

Certain consecutive elements of this list will have identical first and last members (in this example, "a" and "b" in lis[[2]] and lis[[3]]) and I would like to delete the element that has “x” as its middle element, to give:

res = {{"b","x","d"},{"a","z","b"},{"a","x","c"},{"b","z","d"}}

It seems like a job for SequenceCases, but am having no luck.

2 Answers

SequenceReplace[lis, {OrderlessPatternSequence[{a_, "x", c_}, {a_, b_, c_}]} :> {a, b, c}]

{{"b", "x", "d"}, {"a", "z", "b"}, {"a", "x", "c"}, {"b", "z", "d"}}

Correct answer by kglr on April 23, 2021

One approach is to look at the differences between adjacent elements, find those which equal {0,0,x_}, and remove them from the list.

lis[[Complement[Range[Length[lis]],Flatten@Position[Differences[lis], {0, 0, x_}]]]]

{{"b", "x", "d"}, {"a", "z", "b"}, {"a", "x", "c"}, {"b", "z", "d"}}

Answered by bill s on April 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP