我在int
变量中有tracksList_filtered
个数组:
[10422, 10681, 10421, 10392, 10616, 10589, 10581, 10423, 10743, 10213, 10613, 10609, 10427, 10484, 10031, 10169, 10695, 10580, 10171, 10703, 10486, 10631, 10642, 10137, 10566, 10704, 10420, 10525, 10209, 10658, 10617, 10127, 10128, 10391, 10602, 10587, 10030, 10393, 10660, 10614, 10485, 10523, 10215, 10029, 10655, 10210, 10659, 10041, 1075, 10425, 10724, 1068, 10657, 10216, 10662, 10211, 10410, 10601, 10644, 10212, 10074, 10696, 10424, 10208, 1074, 10394, 10419, 10426, 10705, 10038, 10661, 10040, 10165, 10396, 10168, 10653, 10610]
我需要从中删除10422, 10681, 10421
(而非索引)。
我写了下一段代码:
auto tracksList_filtered = result.array.map!(a => a[0].coerce!int);
writeln(tracksList_filtered);
auto x = tracksList_filtered.array.remove(10422, 10681, 10421);
writeln(x);
它迷恋错误:range is smaller than amount of items to pop
来自docs的示例说: "可以将多个索引传递给remove。在这种情况下,各个索引处的元素都被删除。索引必须按递增顺序传递,否则会发生异常。"
int[] a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
assert(remove(a, 1, 3, 5) == [ 0, 2, 4, 6, 7, 8, 9, 10 ]);
我试图检查它并按顺序放置要删除的元素:
auto x = tracksList_filtered.array.remove(1068,1074);
但同样的例外。
答案 0 :(得分:3)
@ user1432751对setDifference
的建议是一个不错的选择,但如果您不想对列表进行排序,则可以这样做:
auto vals = [1,2,3,4,5,6];
auto toRemove = [2,3,5];
auto res = vals.remove!(x => toRemove.canFind(x));
assert(res == [1,4,6]);
答案 1 :(得分:2)
setDifference
auto x = setDifference(tracksList_filtered.sort(), [1068, 1074, 1075].sort());
元素必须进行排序。
答案 2 :(得分:1)
你拥有的是数组中的值,而不是索引,所以你想使用remove的谓词版本:
http://dlang.org/phobos/std_algorithm_mutation.html#.remove.2
所以试试
your_array = your_array.remove!((a) => (a == 10422 || a == 10681 || a == 10421));
因此,您传递的谓词会将值与您要删除的值进行比较。