5
$\begingroup$

There is some lists and this is consisting of {a,{b,c},d,e} elements. For example,

list[1]={{7,{1,1},1,2},{10,{6,2},3,5},{10,{7,2},2,1}}

I want to create new lists, It' s like these elements are d>1, a is replaced by the value e*a(when d=1 there is unchanged in the elements). In the case of list[1],newlist[1]={{7,{1,1},1,2},{10*5,{6,2},3,5},{10*1,{7,2},2,1}}

So,I started writing from If[list[[t,3]]>1,~~~~. (t is Length[list])

However, I got an error saying that expression t cannot be partially specified. How can I help you?

$\endgroup$
1
  • $\begingroup$ Thank you all for all kinds of answers!! $\endgroup$
    – hare
    Commented Sep 19, 2023 at 4:51

7 Answers 7

5
$\begingroup$
lst = {{7, {1, 1}, 1, 2}, {10, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}};

1.

ClearAll[f1]
f1[{a_, {b_, c_}, d_, e_}] := {a If[d > 1, e, 1], {b, c}, d, e}

newlist = Map[f1] @ lst
{{7, {1, 1}, 1, 2}, {50, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}}

2.

ClearAll[f2]
f2 [a_, {b_, c_}, d_, e_] := {a If[d > 1, e, 1], {b, c}, d, e}

newlist = MapApply[f2] @ lst
{{7, {1, 1}, 1, 2}, {50, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}}

3.

You can also use ReplaceAll as follows:

newlst = lst /. {a_, {b_, c_}, d_, e_} /; d > 1 :> {a e, {b, c}, d, e }
{{7, {1, 1}, 1, 2}, {50, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}}
$\endgroup$
5
$\begingroup$
lst = {{7, {1, 1}, 1, 2}, {10, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}};

SequenceCases[lst, {{a_, b_List, d_, e_}} :>  {If[d > 1, a e, a], b, 
   d, e}]

or

{If[#3 > 1, #1 #4, #1], #2, #3, #4} & @@@ lst

Result

{{7, {1, 1}, 1, 2}, {50, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}}

$\endgroup$
5
$\begingroup$

Using Cases:

Cases[lista, x : {a_, {b_, c_}, d_, e_} :> If[d > 1, {a e, {b, c}, d, e}, x]]

(**{{7, {1, 1}, 1, 2}, {50, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}})
$\endgroup$
5
$\begingroup$

Using Parts of Matrices:

(* 1:1 copy of lst *)
newlist = lst; 

(* 1st column transformation based on the values of the 1st, 3rd and 4th columns*)
newlist[[;; , 1]] = #1 If[#2 > 1, #3, 1] & @@@newlist[[;; , {1, 3, 4}]] 

{7, 50, 10}

(* Output newlist *)
newlist

{{7, {1, 1}, 1, 2}, {50, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}}

$\endgroup$
4
$\begingroup$
foo[a : {__, 1, _}] := a
foo[{a_, b__, c_}] := {a c, b, c}

foo /@ lst

{{7, {1, 1}, 1, 2}, {50, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}}

$\endgroup$
4
$\begingroup$

Following the method attempted by OP:

lst = {{7, {1, 1}, 1, 2}, {10, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}}

Table[If[e[[3]] > 1, {e[[1]] e[[4]], e[[2]], e[[3]], e[[4]]}, e], {e, lst}]

{{7, {1, 1}, 1, 2}, {50, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}}

Table[If[e[[3]] > 1, ReplacePart[e, 1 -> e[[1]] e[[4]]], e], {e, lst}]

{{7, {1, 1}, 1, 2}, {50, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}}

$\endgroup$
2
$\begingroup$
list = {{7, {1, 1}, 1, 2}, {10, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}};

Using Replace

Replace[list, {a_, b_, c_, d_} :> {If[c > 1, a d, a], b, c, d}, {1}]

{{7, {1, 1}, 1, 2}, {50, {6, 2}, 3, 5}, {10, {7, 2}, 2, 1}}

$\endgroup$

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.