下面的代码有效,但我对是否有更短/更优雅/更“Pythonic”的方式来编写以下代码感兴趣:
#!/usr/bin/env python
from sortedcontainers import SortedSet
def removeSubsumed(L, R):
"""Returns those lists in L that are not supersets of any list in R"""
result = []
for l in L:
fg = True
for r in R:
if l >= r:
fg = False
break
if fg:
result.append(l)
return result
print removeSubsumed([ SortedSet([ 'a', 'b', 'c' ]),
SortedSet([ 'd', 'e', 'f' ]),
SortedSet([ 'g', 'h', 'i' ]) ],
[ SortedSet([ 'a', 'b' ]),
SortedSet([ 'g' ]) ])
请您参考如下方法:
您可以使用 any()和列表理解。示例 -
def removeSubsumed(L, R):
return [s for s in L if not any(s >= r for r in R)]
any() - 如果任何元素为 True,则返回 True,否则返回 False(即使它作为参数接收的 iterable 为空)。
