0
|
7055 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
GROUPS:

# What is the best way to completely remove brackets in parts ofan expression

Posted 9 years ago
 Hello A simple example :  data = {a, b, c, d}; {15, With[{k = c}, Flatten[Position[data, k]]]} (* output {15,{3}} *)  To remove the brackets around 3 I found no other way than: {15, With[{k = c}, Flatten[Position[data, k]]] /. {a_} -> a} (* output {15,3} *)  Are there better ways to get this result? The most frequent use I have for this construction is to get iterators values for Sum or Product that are returned by various functions and of course they are always between brackets. Thanks
3 Replies
Sort By:
Posted 9 years ago
 I like. Thanks
Posted 9 years ago
 In:= (* in your example *) {15, {3}} /. {a_, {b_}} -> {a, b} Out= {15, 3} In:= (* more generally *) range = {-2, 3}; Sum[x^2, {x, Sequence @@ range}] Out= 19 
Posted 9 years ago
 If you are sure that c only occurs once, you can do it also with data = {a, b, c, d}; Flatten[{15, With[{k = c}, Position[data, k]]}] or with data = {a, b, c, d}; {15, With[{k = c}, Position[data, k]][[1, 1]]}