1
$\begingroup$

I have a nested list in which part 2 of each sublist repeats itself a number of times before changing, i need an if condition for when part 2 of the next sublist changes from the prevous sublist.

ex. {{2,2},{3,2},{4,2},{2,3},{3,3,}}, part 2 of the 4. sublist changes.

(note that the nested list is much longer and the sublists change a number of times).

I hope my question is understandable.

$\endgroup$
2
  • 1
    $\begingroup$ No, it is unclear. What do you want to do with this If operator? What is your aim? $\endgroup$ Nov 23, 2015 at 14:48
  • $\begingroup$ i need a conditon for an if statement dependent on the change of part 2 of a sublist in my nested list. So every time part 2 changes from the previous pattern, add the string "Z0,05" to part 2.. But its only the conditon that i have issues with. Though i think i can use eldos answer. $\endgroup$
    – Morphalog
    Nov 23, 2015 at 15:11

1 Answer 1

1
$\begingroup$
list = {{2, 2}, {3, 2}, {4, 2}, {2, 3}, {3, 3}};

SplitBy[list, Last]

{{{2, 2}, {3, 2}, {4, 2}}, {{2, 3}, {3, 3}}}

If you want to know the positions where the 2nd element changes:

list = {{2, 2}, {3, 2}, {4, 2}, {2, 3}, {3, 3}, {3, 4}};

Position[Partition[Last /@ list, 2, 1], {a_, b_} /; a != b] + 1

{{4}, {6}}

$\endgroup$
2
  • $\begingroup$ thx of the help, i think i can use this. $\endgroup$
    – Morphalog
    Nov 23, 2015 at 15:07
  • $\begingroup$ How would the code change for a list with 3 element arrays? {{{2, 2, 2}, {3, 2, 3}, {4, 2, 2}}, {{2, 3,2 }, {3, 3, 6}}} $\endgroup$
    – SPIL
    Dec 7, 2016 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.