Access and select specific list items using Pure Function

  • Part I: ACCESS

Provided by:

list = {{z, x, c, d}, {1, 2, 3, 4}}

I would like to do the following:

(#3/2 + #1/3) &[list[[1]]]

Unfortunately, the result:

enter image description here

So far my desired result is:

enter image description here

obtained using:

 (#3/2 + #1/3) &[z, x, c, d]
  • Part II: AIR CONDITIONING

Trying to do this:

Select[list[[2]], # > 2 &] 

How can I specify a C # sublist if possible?

Answer kindly provided by Leonid (described in detail below):

Select[#[[2]], # > 2 &] &[list]
+3
source share
4 answers

You were almost there:

(#[[3]]/2 + #[[1]]/3) &[list[[1]]]

# 1 is the first argument to the function, and the third is the third. You provide only one argument, namely the list [[1]]. Since the list [[1]] is a list, it is displayed above your function. # [[[1]] and # [[3]] indicate the first and third part / element of the first argument.

+5

Sequence: (#3/2 + #1/3) &[Sequence @@ (list[[1]])] , . Sequence

+3

Sometimes you may find it helpful to use some notation to improve readability. For example, specifying Marcus's answer:

list = {{z, x, c, d}, {1, 2, 3, 4}}
third[x_List] := x[[3]];

third@#/2 + First@#/3 &@ First@list
(*
-> c/2+z/3
*)
+3
source

For the first, you just need Apply(short form @@):

#3/2 + #1/3 & @@ list[[1]]
+3
source

All Articles