在Python 3中是否有一個函數可以讓我在大字符串中以任何順序查找子字符串?例如:在混亂的字符串中查找字符串
ant in Gnat returns True
flat in plat returns False
cooler in polomacear returns True
在Python 3中是否有一個函數可以讓我在大字符串中以任何順序查找子字符串?例如:在混亂的字符串中查找字符串
ant in Gnat returns True
flat in plat returns False
cooler in polomacear returns True
這是我鞭策的事情,它會做到這一點。可能會更容易,但它通過測試!
def check(sub, full):
full_list = list(full)
for char in sub:
if char in full_list:
full_list.remove(char)
else:
return False
return True
這工作完美! – 2014-09-27 14:50:34
對於O(n * n)行爲足夠長的字符串而言,collections.Counter可以在兩個字符串上使用。然後看看子多重集是否是完整多重集的子多組。 – 2014-09-28 21:45:11
你可能是想自己想出點什麼。 – usr2564301 2014-09-27 13:52:55