【发布时间】:2021-02-23 11:07:46
【问题描述】:
我需要检查一个字符串是否存在于更大的字符串集中,如果它确实存在,则将包含它的字符串添加到另一个列表中。我有检查存在的代码,它可以正常工作,但由于我的实现,它无法添加字符串。
代码
test_list = ['temp', 'temperature']
b = ['sunny', 'cloudy', 'stempy', 'temp','newtemperature']
hits = []
hit_name = []
for test_string in b:
res = any(item in test_string for item in test_list)
if res == True:
hit_name.append(item)
hits.append(res)
# print result
print('\n'*2, hits)
期望的输出
hit_name = ['stempy', 'temp','newtemperature']
【问题讨论】: