3
$\begingroup$

I am having a lot of issues trying to import data into mathematica and then clean the data so that mathematica recognises it. The data is a set of permutations, which I would like to express as Cycles.

This is how (a section) of the data appears in the .txt file:

(),(20,21),(19,20),(19,20,21),(19,21,20),(19,21),(17,18),(17,18)(20,21),(17,18)(19,20),(17,18)(19,20,21),

(17,18)(19,21,20),(17,18)(19,21),(16,17),(16,17)(20,21),(16,17)(19,20),(16,17)(19,20,21),(16,17)(19,21,20),

(16,17)(19,21),(16,17,18),(16,17,18)(20,21),(16,17,18)(19,20),(16,17,18)(19,20,21),(16,17,18)(19,21,20),

When I import this into mathematica via:

list=Import["pathname.txt","Data"]//InputForm

This outputs the following:

{{"(),(20,21),(19,20),(19,20,21),(19,21,20),(19,21),(17,18),(17,18)(20,21),(17,18)(19,20),(17,18)(19,20,21),"}, 
 {"(17,18)(19,21,20),(17,18)(19,21),(16,17),(16,17)(20,21),(16,17)(19,20),(16,17)(19,20,21),(16,17)(19,21,20),"}, 
 {"(16,17)(19,21),(16,17,18),(16,17,18)(20,21),(16,17,18)(19,20),(16,17,18)(19,20,21),(16,17,18)(19,21,20),"}, 

I am not able to get mathematica to remove the line breaks, so that it reads as one long list of permutations, seperated only by commas. I have tried StringRemove StringReplace with no luck. I have also attempted ImportString[ToString[list],"CSV"] but this has not worked either.

I cannot manually remove the line breaks in the .txt file as the actual set of permutations is far too large.

$\endgroup$
2
  • $\begingroup$ Perhaps try, Flatten[list] prior to string operations. $\endgroup$ Commented Jul 16 at 11:02
  • 2
    $\begingroup$ Import it as a pure text, namely t = Import[file, "String"], then you should be able to do StringReplace[t, "\n" -> ""]. $\endgroup$
    – Domen
    Commented Jul 16 at 11:16

2 Answers 2

3
$\begingroup$

Maybe a little bit laborious, but it works (at least for the given examples):

imp = StringReplace[Import["Cycles_test.txt", "Text"], WhitespaceCharacter .. -> ""]
(* 
"(),(20,21),(19,20),(19,20,21),(19,21,20),(19,21),(17,18),(17,18)(20,21),(17,18)(19,20),(17,18)(19,20,21),(17,18)(19,21,20),(17,18)(19,21),(16,17),(16,17)(20,21),(16,17)(19,20),(16,17)(19,20,21),(16,17)(19,21,20),(16,17)(19,21),(16,17,18),(16,17,18)(20,21),(16,17,18)(19,20),(16,17,18)(19,20,21),(16,17,18)(19,21,20)"
*)

then multiple string replacements:

cycleStrings = StringReplace[StringReplace[StringReplace[StringSplit[StringReplace[imp, "),(" -> ");("], ";"], {"(" -> "{",")" -> "}"}], "}{" -> "},{"], s__ :> "{" ~~ s ~~ "}"]

and finally:

Cycles[ToExpression[#]] & /@ cycleStrings

yields:

{Cycles[{}], Cycles[{{20, 21}}], Cycles[{{19, 20}}], 
 Cycles[{{19, 20, 21}}], Cycles[{{19, 21, 20}}], Cycles[{{19, 21}}], 
 Cycles[{{17, 18}}], Cycles[{{17, 18}, {20, 21}}], 
 Cycles[{{17, 18}, {19, 20}}], Cycles[{{17, 18}, {19, 20, 21}}], 
 Cycles[{{17, 18}, {19, 21, 20}}], Cycles[{{17, 18}, {19, 21}}], 
 Cycles[{{16, 17}}], Cycles[{{16, 17}, {20, 21}}], 
 Cycles[{{16, 17}, {19, 20}}], Cycles[{{16, 17}, {19, 20, 21}}], 
 Cycles[{{16, 17}, {19, 21, 20}}], Cycles[{{16, 17}, {19, 21}}], 
 Cycles[{{16, 17, 18}}], Cycles[{{16, 17, 18}, {20, 21}}], 
 Cycles[{{16, 17, 18}, {19, 20}}], Cycles[{{16, 17, 18}, {19, 20, 21}}],
  Cycles[{{16, 17, 18}, {19, 21, 20}}]}

This could be converted into permutations by PermutationList:

{{}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
  19, 21, 20}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
   17, 18, 20, 19}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
  15, 16, 17, 18, 20, 21, 19}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
   13, 14, 15, 16, 17, 18, 21, 19, 20}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 
  10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 20, 19}, {1, 2, 3, 4, 5, 6, 
  7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 17}, {1, 2, 3, 4, 5, 6, 7, 
  8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 17, 19, 21, 20}, {1, 2, 3, 4, 
  5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 17, 20, 19}, {1, 2, 
  3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 17, 20, 21, 
  19}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 17,
   21, 19, 20}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
  16, 18, 17, 21, 20, 19}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
   14, 15, 17, 16}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
  15, 17, 16, 18, 19, 21, 20}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
   13, 14, 15, 17, 16, 18, 20, 19}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
  11, 12, 13, 14, 15, 17, 16, 18, 20, 21, 19}, {1, 2, 3, 4, 5, 6, 7, 
  8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 18, 21, 19, 20}, {1, 2, 3, 4, 
  5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 18, 21, 20, 19}, {1, 
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 16}, {1, 2, 
  3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 16, 19, 21, 
  20}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 16,
   20, 19}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 
  18, 16, 20, 21, 19}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
   15, 17, 18, 16, 21, 19, 20}}
$\endgroup$
4
$\begingroup$

Another way:

dataFile = (* String representation of input file *)
  "(),(20,21),(19,20),(19,20,21),(19,21,20),(19,21),(17,18),(17,18)(20,21),(17,18)(19,20),(17,18)(19,20,21),
(17,18)(19,21,20),(17,18)(19,21),(16,17),(16,17)(20,21),(16,17)(19,20),(16,17)(19,20,21),(16,17)(19,21,20),
(16,17)(19,21),(16,17,18),(16,17,18)(20,21),(16,17,18)(19,20),(16,17,18)(19,20,21),(16,17,18)(19,21,20),";
input = ImportString[dataFile, "Text"];

Then after Import[] file as "Text":

StringCases[input,
 cycs : ("(" ~~ RegularExpression["[^()]*"] ~~ ")") .. :>
  Cycles[ToExpression[
    "{" <> StringReplace[cycs, {")(" -> "},{", "(" -> "{", ")" -> "}"}] <> "}"]]
 ]
(*
{Cycles[{}], Cycles[{{20, 21}}], Cycles[{{19, 20}}], 
 Cycles[{{19, 20, 21}}], Cycles[{{19, 21, 20}}], Cycles[{{19, 21}}], 
 Cycles[{{17, 18}}], Cycles[{{17, 18}, {20, 21}}], 
 Cycles[{{17, 18}, {19, 20}}], Cycles[{{17, 18}, {19, 20, 21}}], 
 Cycles[{{17, 18}, {19, 21, 20}}], Cycles[{{17, 18}, {19, 21}}], 
 Cycles[{{16, 17}}], Cycles[{{16, 17}, {20, 21}}], 
 Cycles[{{16, 17}, {19, 20}}], Cycles[{{16, 17}, {19, 20, 21}}], 
 Cycles[{{16, 17}, {19, 21, 20}}], Cycles[{{16, 17}, {19, 21}}], 
 Cycles[{{16, 17, 18}}], Cycles[{{16, 17, 18}, {20, 21}}], 
 Cycles[{{16, 17, 18}, {19, 20}}], Cycles[{{16, 17, 18}, {19, 20, 21}}],
 Cycles[{{16, 17, 18}, {19, 21, 20}}]}
*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.