有没有什么列表推导技巧可以更好地得到以下所需结果?
a = ['hello', 'world', 'hello world', 'hello world how are', 'hello india']
final = set()
for i in a:
for j in [x for x in a if x != i]:
if i in j:
final.add(i)
list(set(a)^final)
if
语句中加入break
- 没有必要将任何项添加到final
中超过一次。 - jasonharper