我有几个这样的功能:
object obj.getChild(childIndex)
int obj.numChildren()
所以我使用它们来创建这个函数:
collection obj.getChildren()
我在返回类型中很灵活,但我会使用另一个列表做很多“减法”,“乘法”。所以像这样:
children = obj.getChildren()
children = [5,10,15,20,25]
globalChildren = [1,2,3,4,5,6,7,8,9,10,12,14,16,18,20]
difference = children - globalChildren
difference = [15,25]
shared = children * globalChildren
shared = [5,10,20]
有没有一种快速而优雅的方法来做这些或者我必须逐个浏览每个元素并手动收集元素?
答案 0 :(得分:3)
您正在寻找set
s
children = {5,10,15,20,25}
globalChildren = {1,2,3,4,5,6,7,8,9,10,12,14,16,18,20}
difference = children - globalChildren
shared = children & globalChildren