I have a text file which is a list of numbers and would like to pass each number in the list as input to a Mathematica function.

For example, my text might contain [2, 1, 4], and I would like to pass this into CompleteGraph, so that it returns, CompleteGraph[2], CompleteGraph[1], CompleteGraph[4].

How might I do this? I come from python and think a for loop would work but I don't have any idea about how to do it in Mathematica.

  • $\begingroup$ Import["file.txt"] $\endgroup$ – cvgmt Oct 9 at 12:35
  • 1
    $\begingroup$ If your text contains "[..]" as list separators, you must them first change to braces by: "StringReplace[string,{"["->"{", "]"->"}"}] " and then you can evaluate the string ny: "Evaluate[string]". Having created this list of data, you can feed it to "CompleteGraph[" $\endgroup$ – Daniel Huber Oct 9 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.