groovy 两个list string 其中一个代表所有另一个代表已使用 生成出未使用的list
以下是用 Python 语言实现的示例代码:
all_list = ['apple', 'banana', 'orange', 'pear', 'peach']
used_list = ['banana', 'pear']
unused_list = [item for item in all_list if item not in used_list]
print(unused_list)
输出结果为:
['apple', 'orange', 'peach']
解释一下代码:
首先定义了两个列表 all_list 和 used_list,分别代表所有的可用项和已经被使用过的项。
然后使用了列表推导式,遍历了 all_list 中的每一个元素,如果它不在 used_list 中,那么就将它加入到 unused_list 中。
最后输出了 unused_list。
原文地址: https://www.cveoy.top/t/topic/gBsw 著作权归作者所有。请勿转载和采集!