# 收集由范围列表定义的列表的所有元素

``````a = {{1}, {2}, {3}, {4},{5},{6}}
``````

``````range = {{1, 3}, {4, 6}};
``````

``````result = {{1,2,3},{4,5,6}}
``````

``````t = {};
result = AppendTo[t, a[[#[[1]], #[[2]] ]] ] & /@ a
``````

5

## 6 答案

``````Flatten[Take[a, #]] & /@ range
``````

{{1,2,3}，{4,5,6}}

8

``````result = Range @@@ range
``````

{{1,2,3}，{4,5,6}}

``````a = {{1}, {2}, {3}, {4}, {5}, {6}};
range = {{1, 3}, {4, 6}};
t = {};
result = Last[AppendTo[t, a[[#[[1]] ;; #[[2]], 1]]] & /@ range]
``````
6

@totyped对于 `a = {{1}，{2}，{3}，{4}，{5}，{6}}` ，您需要输出 `{{1,2， 3}，{4,5,6}}``a = {1,2,3,4,5,6}` 我不明白输出应该如何改变。输出应该是什么？

@totyped这就是为什么我不在第一个中使用 `a` 。我第二个你需要删除`，1`

``````a = {{1}, {2}, {3}, {4}, {5}, {6}}//Flatten;

range = {{1, 3}, {4, 6}};

Pick[a, #] & /@ Outer[IntervalMemberQ[Interval[#1], #2] &,
range, a, 1]

(* {{1, 2, 3}, {4, 5, 6}} *)
``````
5

“我想收到一个列表，该列表在 `range` ”范围内收集列表a的所有列表元素

``````SeedRandom[1]
a = [email protected][List /@ Range[20], 10]
range = {{1, #}, {# + 1, 20}} &@ RandomInteger[{Min[a] + 1, Max[a] - 2}]

(* {{1}, {2}, {6}, {8}, {9}, {11}, {13}, {16}, {17}, {19}} *)

(* {{1, 5}, {6, 20}} *)
``````

“正确”输出（再次，根据我，可能不是OP）应该是

``````(* {{1, 2}, {6, 8, 9, 11, 13, 16, 17, 19}} *)
``````

（据我所知）只有@ Nasser的 `Map` 和@BobHanlon的 `Pick` 提供。 （显然，这在某种程度上仍然“很好” - 我假设 `a` 是整数的有序列表， `range` 仅由两个间隔组成，等等。 。）

``````Pick[Flatten[a], UnitStep[Flatten[a] - range[[2, 1]]], #] & /@ {0, 1}

TakeDrop[#, [email protected][#, x_ /; x > range[[1, 2]]] - 1] &@ Flatten[a]

GatherBy[Flatten[a], # <= range[[1, 2]] &]

[email protected][Scan[Sow[#, # <= range[[1, 2]]] &, Flatten[a]]]
``````

（他们还为问题中的示例提供了正确的输出。）

5

Using `Intersection`

``````a = [email protected]{{1}, {2}, {3}, {4}, {5}, {6}};
range = Range @@@ {{1, 3}, {4, 6}};
``````

``````Map[Intersection[a, #] &, range]
``````

``````a = [email protected]{{1}, {2}, {5}, {6}};
``````

``````Map[Intersection[a, #] &, range]
``````

4

``````Flatten[a[[Span @@ #]]] & /@ range
``````

{{1,2,3}，{4,5,6}}

``````Extract[a, List /@ (Range @@@ range), Flatten]
``````

{{1,2,3}，{4,5,6}}

``````Extract[Range[0, 100, 10], List /@ (Range @@@ range), Flatten]
``````

{{0,10,20}，{30,40,50}}

``````Extract[CharacterRange["A","Z"], List /@ (Range @@@ range), Flatten]
``````

{{“A”，“B”，“C”}，{“D”，“E”，“F”}}

4