我正在尝试查找字符串列表中是否存在子字符串,例如:我有单词列表 ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR']PINK 是 ASDEKNIP 的一个子串,因为 PINK 的反面是 KNIP 而单词 BAR 在 WORDRRAB 中,因为反面是 RAB如何查找子带是否退出?如果是,那么把那个字符串倒过来,这样新列表应该是:d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']我试过这样d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR']for word in d: word = word[::-1] if word in d: print(word)但它什么也没给
1 回答

月关宝盒
TA贡献1772条经验 获得超5个赞
使用itertools.permutations:
from itertools import permutations
d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR']
for x, y in permutations(d, 2):
rev = y[::-1]
if rev in x:
d.append(rev)
print(d)
# ['GGBASDEPINK', 'ASDEKNIP', 'PINK', 'WORDRRAB', 'BAR', 'KNIP', 'RAB']
添加回答
举报
0/150
提交
取消