mathematica 如何在数组中等间隔插入元素
如何 在{a,b,c}中间隔插入 2个100,得到 {a,100,100,b,100,100,c,100,100}
seperate[list_List, seperator_, len_Integer] := Flatten[#, 1] &@ Insert[list, Array[seperator &, {len}], List /@ Range[Length@list] + 1]
调用很简单
seperate[{a,b,c},100,3]
更新版
seperate[list_List, seperator_, step_Integer, count_Integer] := Flatten[#, 1] &@ Insert[list, Array[seperator &, {count}], List /@ Range[Quotient[Length@list, step]]*step + 1] seperate[CharacterRange["a", "z"], 0, 5, 1]
输出
{"a", "b", "c", "d", "e", 0, "f", "g", "h", "i", "j", 0, "k", "l", "m", "n", "o", 0, "p", "q", "r", "s", "t", 0, "u", "v", "w", "x", "y", 0, "z"}