0
$\begingroup$

I would like to create an array f containing n indices. The label of those indices is stored in a liste of length n, let's call it "list".

So I would like to have something like :

{f[list[[0]]], f[list[[1]],...}

The point is to affect the f[liste[[i]]] to some values after.

I tried to make a table, but the problem is that the indices cannot be a list from what I have seen (in the sense that they must be regularly spaced).

I also tried to use the array function but I have the same problem, I cannot specify the indices as being a list, it must be a regular spacing.

How can I do it ?

$\endgroup$
3
  • 2
    $\begingroup$ ... also Table[f[i],{i, list}]? $\endgroup$
    – kglr
    Commented Jun 24, 2019 at 9:51
  • $\begingroup$ List indices start with 1 in Mathematica, so list[[0]] is not valid (it refers to the head, which is List). $\endgroup$
    – Roman
    Commented Jun 24, 2019 at 10:32
  • $\begingroup$ Maybe f /@ list would work. $\endgroup$
    – Roman
    Commented Jun 24, 2019 at 10:33

2 Answers 2

1
$\begingroup$

Use AssociationMap. E.g.,

m = AssociationMap[f, {8, 29, 16}]
m[29]  (* f[29] *)
$\endgroup$
1
$\begingroup$

Given a list of integers,

list = RandomInteger[{1, 10}, 10]
(*    {5, 8, 8, 2, 6, 2, 3, 7, 7, 5}    *)

we can construct the required {f[list[[1]]], f[list[[2]], ...} with

f /@ list
(*    {f[5], f[8], f[8], f[2], f[6], f[2], f[3], f[7], f[7], f[5]}    *)
$\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.