You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检查列表A是否包含列表B的元素并提取匹配元素?

提取列表A中包含的列表B元素

需求:给定列表A(文本拆分后的词汇列表)和列表B(敏感词列表),提取列表A中所有存在于列表B中的元素。

用户实现代码:

List<String> profanityList = str.split('\n');

List<String> storyWordList = NewStory.storyController.text.split(' ');

List<String> wordsForChange = [];

for (int i = 0; i < storyWordList.length; i++) 
{
    if (profanityList.contains(storyWordList[i])) 
    {
       wordsForChange.add(storyWordList[i]);
    }
}

简化写法(Dart)

利用Dart集合方法可以让代码更简洁,逻辑和原代码完全一致:

List<String> profanityList = str.split('\n');
List<String> storyWordList = NewStory.storyController.text.split(' ');
List<String> wordsForChange = storyWordList.where((word) => profanityList.contains(word)).toList();

内容的提问来源于stack exchange,提问作者Aleksandr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 18:51:32