0

我有几个这样的功能:

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]

有没有一种快速而优雅的方法来做这些,还是我必须一个一个地浏览每个元素并手动收集元素?

4

1 回答 1

3

你在找sets

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
于 2012-06-23T23:34:29.553 回答