gpt4 book ai didi

dart - 检查一个列表是否包含另一个列表的一个或多个值

转载 作者:行者123 更新时间:2023-12-03 04:35:10 25 4
gpt4 key购买 nike

我们如何在Dart中做到这一点:

List<String> text= ["This", "is", "very", "nice"];

List<String> search1= ["This"];
List<String> search2= ["very", "is"];
List<String> search3= ["This", "is", "great"];

print(text.contains(search1)); // true
print(text.contains(search2)); // true
print(text.contains(search3)); // false
由于上述代码的结果始终为“false”:
https://dartpad.dartlang.org/73f7c386cbb7246c617712c390d4dbb1

最佳答案

您可以使用Set.intersection()

List<String> text = ["This", "is", "very", "nice"];

List<String> search1 = ["This"];
List<String> search2 = ["very", "is"];
List<String> search3 = ["This", "is", "great"];

print(text.toSet().intersection(search1.toSet()).length == search1.length); // true
print(text.toSet().intersection(search2.toSet()).length == search2.length); // true
print(text.toSet().intersection(search3.toSet()).length == search3.length); // false

关于dart - 检查一个列表是否包含另一个列表的一个或多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64287212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com