【发布时间】:2013-02-04 05:00:03
【问题描述】:
今天可能有点太多了..但是嗯。
这个问题让我很困惑。此函数将字符串列表作为参数,并返回作为其前一个字符串的子字符串的每个字符串。所以
- ["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'”我发现唯一有效的结果是 len(a)
【问题讨论】: