Message Boards Message Boards

0
|
7476 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:
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

POSTED BY: wojtek potocki
3 Replies
Posted 9 years ago

I like. Thanks

POSTED BY: wojtek potocki
Posted 9 years ago
In[1]:= (* in your example *)
{15, {3}} /. {a_, {b_}} -> {a, b}

Out[1]= {15, 3}

In[2]:= (* more generally *)
range = {-2, 3};
Sum[x^2, {x, Sequence @@ range}]

Out[3]= 19
POSTED BY: David Keith

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]]}
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract