假设我有一个字符串数组a=['abcbdefcd']和另一个字符串数组 T=['ab','abc','def','cd','abcd']
我想编写一个函数来搜索数组a,如果它在T中找到a中的字符串,则返回true或false。
我想我会试试
其他:
但这并没有给我我想要的东西,有人能给我提些别的建议吗?
要做到这一点,您有各种各样的蟒蛇解决方案:
方法1 :
bool(set(a) & set(b))
a = ['abc','bca','av'] b = ['ab','bc'] print(bool(set(a) & set(b))) # this would return true in this case
另一种方法
a = set(a); any(i in a for i in b)
以及 最后 可以使用冻结集的不相交方法:
not set(a).isdisjoint(b)
这应该能实现你想要的,
A = ['abcbdefcd'] T = ['ab', 'abc', 'def', 'cd', 'abcd'] result = False for text in A: for sub_text in T: if (sub_text in text): result = True break if (result): print('True') else: print('False')
对于您的具体要求:
a = ['abcbdefcd'] t = ['ab', 'abc', 'def', 'cd', 'abcd'] for text in t: print(text in a[0])
输出:
True True True True False
if text in t
if text in t[0]