I’ve declared a list:

list = {{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}, {a, b, c}, {e,

f}, {g}, {}, {Sin[a], Cos[b]}};

now I’ve made an rule which add 2 pairs together:

Cases[list, {x_, y_} -> x + y]

later only Integer numbers:

Cases[list, {x_Integer, y_Integer} -> x + y]

But now i want find the sum of sublists which have the Length >=2 so that the result is:

{3,8,9,6,a+b+c,e+f,Cos[b]+Sin[a]}

and after that I want to find the sum of all sublists so that the result would be:

{3,2,8,9,6,a+b+c,e+f,g,0,Cos[b]+Sin[a]}

Thanks for helping

=================

Cases[list, {x_Integer, y_Integer} -> x + y]

– MarcoB

May 9 at 19:52

the length shouldn’t be lower than >=3

– Katze

May 9 at 19:55

1

list /. {x_Integer, y_Integer} -> {x + y} returns {{8}, {2}, {2, 6, 7}, {5}, {2.5, 3.2}}. However, I am not sure that this is what you want. You really should include your desired result in the question.

– MarcoB

May 9 at 20:03

Yes that is the result what i want. But now i want to do this Cases[list, {x_Integer, y_Integer} -> x + y,Length>=3] so that only the lists which are higher and equal 3 as a sum in a result

– Katze

May 9 at 20:05

1

@Katze. Maybe Select[newlist, Total[{Sequence @@ #}] == 7 &] where newlist = {{1, 2, 4}, 6, 7, 8, {8, 2}, {3, 4}, {4, 5}, {1, 2, 3, 1}};. Output is {{1, 2, 4}, 7, {3, 4}, {1, 2, 3, 1}}. Stephen Wolfram’s book, An Elementary Introduction to the Wolfram Language, which you are probably aware of, explains things much better than I can.

– tomd

May 10 at 0:18

=================

2 Answers

2

=================

You may use Total and Select.

Total /@ Select[Length@# >= 2 &]@list

{3, 8, 9, 6, a + b + c, e + f, Cos[b] + Sin[a]}

Total /@ list

{3, 2, 8, 9, 6, a + b + c, e + f, g, 0, Cos[b] + Sin[a]}

Hope this helps.

2

Instead of mapping: Total[Select[list, Length[#] >= 2 &], {2}].

– J. M.♦

May 10 at 9:06

@J.M. Nice addition. +1

– Edmund

May 10 at 9:14

Since you insist on using Cases:

list = {{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}, {a, b, c},

{e,f}, {g}, {}, {Sin[a], Cos[b]}};

Cases[list, sub : {_, __} :> Total@sub]

{3, 8, 9, 6, a + b + c, e + f, Cos[b] + Sin[a]}

1

Or, as I said in a comment, Cases[list, {x_, y__} :> x + y]

– tomd

May 10 at 9:53

@TomD Sorry, missed that comment, just read first two and last two of them :/

– Kuba

May 10 at 9:54