如何从 D 中的数组中删除元素
How to remove elements from array in D
我在 tracksList_filtered
变量中得到了 int
的数组:
[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 的例子说:
"Multiple indices can be passed into remove. In that case, elements at the respective indices are all removed. The indices must be passed in increasing order, otherwise an exception occurs."
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);
但是遇到了同样的异常。
你拥有的是数组中的值,而不是索引,所以你想使用 remove 的谓词版本:
http://dlang.org/phobos/std_algorithm_mutation.html#.remove.2
所以试试
your_array = your_array.remove!((a) => (a == 10422 || a == 10681 || a == 10421));
因此您传递的谓词会将值与您要删除的值进行比较。
问题已解决setDifference
auto x = setDifference(tracksList_filtered.sort(), [1068, 1074, 1075].sort());
元素必须排序。
@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]);
我在 tracksList_filtered
变量中得到了 int
的数组:
[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 的例子说: "Multiple indices can be passed into remove. In that case, elements at the respective indices are all removed. The indices must be passed in increasing order, otherwise an exception occurs."
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);
但是遇到了同样的异常。
你拥有的是数组中的值,而不是索引,所以你想使用 remove 的谓词版本:
http://dlang.org/phobos/std_algorithm_mutation.html#.remove.2
所以试试
your_array = your_array.remove!((a) => (a == 10422 || a == 10681 || a == 10421));
因此您传递的谓词会将值与您要删除的值进行比较。
问题已解决setDifference
auto x = setDifference(tracksList_filtered.sort(), [1068, 1074, 1075].sort());
元素必须排序。
@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]);