从字符串列表中移除字符串列表

huangapple go评论56阅读模式
英文:

Remove list of strings from list of strings

问题

以下是翻译好的代码部分:

我有一个长字符串列表主列表和一个短字符串列表短列表是一个排除列表我想从主列表中删除所有在排除列表中的元素

我找到了两种方法来做这个但似乎都不起作用

val fnrliste: MutableList<String> = ArrayList()

val excludeList = listOf("28030140259", "12050101833", "21089233132", "12050101833")

// 方法1:
for (fnr in excludeList) { fnrliste.remove(fnr) }

// 方法2:
fnrliste.removeAll(excludeList)

希望这有所帮助。如果您需要进一步的解释或帮助,请随时提出。

英文:

I have a long list of strings (the main list), and a short list of strings. The short list is an exclude list, and I want to remove all occurences of the elements in the exclude list from the main list.

I've found these two ways of doing it, but none of them seem to work:

val fnrliste: MutableList&lt;String&gt; = ArrayList()

val excludeList = listOf(&quot;28030140259&quot;, &quot;12050101833&quot;, &quot;21089233132&quot;, &quot;12050101833&quot;)

//Alternative 1:
for (fnr in excludeList) { fnrliste.remove(fnr) }

//Alternative 2:
fnrliste.removeAll(excludeList)

With both alternatives, the strings in the entries are still there when I list the contents of fnrliste.

As can be seen in this screenshot, some entries have been removed (different result with the two methods), but the first entry in the exclude list is still present:

从字符串列表中移除字符串列表

What am I missing here?

答案1

得分: 0

看起来你的fnrliste包含一些重复项,这就是为令你得到不同的结果的原因。此外,就我记得的你的屏幕截图而言,在removeAll的情况下,第一个元素是"28030140259 "而不是"28030140259",所以一切都是正确的。

英文:

Looks like your fnrliste contains some duplicates, so that's why you get different result. Also, as far as I can remember your screenshot, in case of removeAll the first element is &quot;28030140259 &quot; and not &quot;28030140259&quot; so everything is correct

huangapple
  • 本文由 发表于 2023年2月9日 01:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定