今天可能有点太多了..但是嗯。
这个问题让我很困惑。此函数将字符串列表作为参数,并返回作为其前一个字符串的子字符串的每个字符串。所以
- ["hope", "hop", "hopefully", "test", "testing"] 将返回 ['hop']
- ["hopefully", "hope", "hop", "testing", "test"] 将返回 ['hope', 'hop', 'test']
请原谅这里的代码混乱,我还在学习。
def findSubStrs(lst):
'list ==> list, return list of all strings that are substrings of their predecessor in lst'
res = []
for a in lst:
if len(int(a-1)) > len(lst):
res = res + [a]
return res
我认为 len(int(a-1)) 可以检查前面的字符串,但我刚刚收到错误消息“TypeError: unsupported operand type(s) for -: 'str' and 'int'” The only result我发现工作是 len(a) < 3 或其他一些 int,但这并没有返回我需要的一切。