Guest User

Untitled

a guest
Feb 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. list={{0,...},{1,...},{1,...},{0,...},{3,...},{3,...},{0,...},{0,...},{5,...},{5,...},{5,...},{0,...},{5,...},{0,...},...}
  2.  
  3. list[[All,1]]
  4. (* {0,1,1,0,3,3,0,0,5,5,5,0,5,0,...} *)
  5.  
  6. {0,1,1,0,3,3,0,0,5,5,5,5,0,0,...}
  7.  
  8. 3, 1, 5, 4, 6, ...
  9.  
  10. sortedPositions[list_List] :=
  11. Module[{tmp = list[[All, 1]], length, pos, tmp1, tmp2,
  12. tmp3},
  13. length = Length[tmp];
  14. tmp1 = List /@ Cases[Transpose[{tmp, Range[length]}], {0, _}];
  15. tmp2 = DeleteCases[Transpose[{tmp, Range[length]}], {0, _}];
  16. tmp3 = GatherBy[tmp2, First];
  17. tmp2 = Join[tmp1, tmp3];
  18. Flatten[SortBy[tmp2, #[[1, 2]] &], 1][[All, 2]]
  19. ];
  20.  
  21. num = 20;
  22. testList = Join[List /@ RandomInteger[{0, 9}, num], RandomReal[{0, 1}, {num, 6}], 2]
  23.  
  24. (*
  25. {{6,0.456203,0.0900917,0.62677,0.638615,0.227849,0.61252},
  26. {4,0.317069,0.44889,0.456945,0.05121,0.940742,0.495415},
  27. {7,0.573698,0.381817,0.859495,0.517238,0.459022,0.957771},
  28. {5,0.832945,0.867634,0.0843833,0.296803,0.944986,0.563913},
  29. {1,0.598743,0.803861,0.082542,0.138926,0.630364,0.0445202},
  30. {7,0.289183,0.257115,0.358083,0.677393,0.206347,0.987678},
  31. {5,0.947487,0.320408,0.600928,0.0718489,0.976703,0.449376},
  32. {0,0.0996927,0.210278,0.408291,0.861885,0.946081,0.0522955},
  33. {0,0.537572,0.160541,0.212737,0.508406,0.353786,0.479605},
  34. {7,0.0815373,0.0677839,0.388955,0.681041,0.795607,0.404398},
  35. {4,0.18704,0.253819,0.141732,0.43889,0.931269,0.556534},
  36. {2,0.262136,0.110553,0.60296,0.482498,0.693049,0.430039},
  37. {5,0.569696,0.262133,0.397575,0.246202,0.499777,0.073326},
  38. {6,0.487893,0.121165,0.413376,0.874849,0.836484,0.792685},
  39. {0,0.677934,0.543956,0.593967,0.138832,0.896184,0.604194},
  40. {2,0.138691,0.150235,0.614355,0.326924,0.615902,0.900494},
  41. {0,0.0254698,0.258354,0.377134,0.569083,0.0925844,0.672802},
  42. {7,0.354392,0.976598,0.658138,0.124943,0.39485,0.239671},
  43. {2,0.622461,0.195612,0.997663,0.421797,0.130802,0.110463},
  44. {2,0.136431,0.799215,0.698071,0.0599957,0.452992,0.378609}} *)
  45.  
  46. positions = sortedPositions[testList]
  47. (* {1, 14, 2, 11, 3, 6, 10, 18, 4, 7, 13, 5, 8, 9, 12, 16, 19, 20, 15,
  48. 17} *)
  49.  
  50. testList[[positions]]
  51. (*
  52. {{6,0.456203,0.0900917,0.62677,0.638615,0.227849,0.61252},
  53. {6,0.487893,0.121165,0.413376,0.874849,0.836484,0.792685},
  54. {4,0.317069,0.44889,0.456945,0.05121,0.940742,0.495415},
  55. {4,0.18704,0.253819,0.141732,0.43889,0.931269,0.556534},
  56. {7,0.573698,0.381817,0.859495,0.517238,0.459022,0.957771},
  57. {7,0.289183,0.257115,0.358083,0.677393,0.206347,0.987678},
  58. {7,0.0815373,0.0677839,0.388955,0.681041,0.795607,0.404398},
  59. {7,0.354392,0.976598,0.658138,0.124943,0.39485,0.239671},
  60. {5,0.832945,0.867634,0.0843833,0.296803,0.944986,0.563913},
  61. {5,0.947487,0.320408,0.600928,0.0718489,0.976703,0.449376},
  62. {5,0.569696,0.262133,0.397575,0.246202,0.499777,0.073326},
  63. {1,0.598743,0.803861,0.082542,0.138926,0.630364,0.0445202},
  64. {0,0.0996927,0.210278,0.408291,0.861885,0.946081,0.0522955},
  65. {0,0.537572,0.160541,0.212737,0.508406,0.353786,0.479605},
  66. {2,0.262136,0.110553,0.60296,0.482498,0.693049,0.430039},
  67. {2,0.138691,0.150235,0.614355,0.326924,0.615902,0.900494},
  68. {2,0.622461,0.195612,0.997663,0.421797,0.130802,0.110463},
  69. {2,0.136431,0.799215,0.698071,0.0599957,0.452992,0.378609},
  70. {0,0.677934,0.543956,0.593967,0.138832,0.896184,0.604194},
  71. {0,0.0254698,0.258354,0.377134,0.569083,0.0925844,0.672802}}
  72. *)
  73.  
  74. group2[lst_] :=
  75. Module[{x, i = 1}, Join @@ GatherBy[lst, #[[1]] /. 0 :> x[i++] &]]
  76.  
  77. big = RandomInteger[5, {10000, 3}];
  78.  
  79. Table[group[big] // Timing // First, {5}]
  80.  
  81. Table[group2[big] // Timing // First, {5}]
  82.  
  83. group[lst_] := Join @@ GatherBy[lst, #[[1]] /. 0 :> Unique[] &]
  84.  
  85. Join[List /@ RandomInteger[{0, 5}, 20], RandomReal[{0, 1}, {20, 6}], 2];
  86.  
  87. %[[All, 1]]
  88.  
  89. group[%%][[All, 1]]
  90.  
  91. {0, 0, 4, 5, 0, 3, 3, 3, 4, 2, 4, 2, 3, 2, 5, 0, 5, 0, 1, 4}
  92.  
  93. {0, 0, 4, 4, 4, 4, 5, 5, 5, 0, 3, 3, 3, 3, 2, 2, 2, 0, 0, 1}
  94.  
  95. list = {0, 1, 1, 0, 3, 3, 0, 0, 5, 5, 5, 0, 5, 0, 1};
  96. Split[list] //. {h___, x : {a_, ___}, m___, y : {a_, ___}, t___} :>
  97. {h, x ~Join~ y, m, t} /; a =!= 0 // Flatten
  98.  
  99. (* {0, 1, 1, 1, 0, 3, 3, 0, 0, 5, 5, 5, 5, 0, 0} *)
  100.  
  101. list={{1,a},{1,b},{0,c},{2,d},{0,e},{2,f},{0,g},{4,h},{0,j}}
  102.  
  103. strangeSort[list_]:=Module[{r},
  104. r=Split[list,#1[[1]]==0&];
  105. r=GatherBy[r,#[[-1,1]]&];
  106. r={#[[1]],Reverse@SortBy[Flatten[#[[2;;-1]],1],#[[1]]]}&/@r;
  107. r=Flatten[r,2]]
  108.  
  109. strangeSort[list]
  110.  
  111. {{1,a},{1,b},{0,c},{2,d},{2,f},{0,e},{0,g},{4,h},{0,j}}
  112.  
  113. GatherByList[list_, representatives_] := Module[{func},
  114. func /: Map[func, _] := representatives;
  115. GatherBy[list, func]
  116. ]
  117.  
  118. g[list_] := Module[{max = Max @ list[[All, 1]] + 1},
  119. Join @@ GatherByList[list, Replace[list[[All, 1]], 0 :> max++, {1}]]
  120. ]
  121.  
  122. list = RandomInteger[5, {10^5, 3}];
  123.  
  124. r1 = g[list]; //AbsoluteTiming
  125. r2 = group2[list]; //AbsoluteTiming
  126.  
  127. r1 === r2
Add Comment
Please, Sign In to add comment