英文:
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<String> = ArrayList()
val excludeList = listOf("28030140259", "12050101833", "21089233132", "12050101833")
//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 "28030140259 "
and not "28030140259"
so everything is correct
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论