-1

假设我有一个字符串 n = '22' 和另一个数字 a = 4,因此 n 是 str 而 a 是 int。我想创建一组列表,例如:

list1 = [22, 12, 2] #decreasing n by 10, the last item must be single digit, positive or 0
list2 = [22, 11, 0] #decreasing n by 11, last item must be single digit, positive or 0
list3 = [22, 21, 20] #decreasing n by 1, last item must have last digit 0
list4 = [22, 13] #decreasing n by 9, last item must be single digit. if last item is == a, remove from list
list5 = [22, 32] #increasing n by 10, last item must have first digit as a - 1
list6 = [22, 33] #increasing n by 11, last item must have first digit as a - 1
list7 = [22, 23] #increasing n by 1, last item must have last digit a - 1
list8 = [22, 31] #increasing n by 9, last item must have first digit a - 1

我正在为如何开始而苦苦挣扎。也许你可以给我一个如何解决这个问题的想法?

顺便说一句,如果不能满足某个条件,那么只有 n 会在该列表中。说 n = '20',a = 4:

list3 = [20]

这也适用于学校项目,用于具有列表项的列表中的索引。我想不出更好的方法来解决这个问题。

4

1 回答 1

1

这应该让你开始:

def lbuild( start, inc, test ):
    rslt = [start]
    while not test(start,inc):
        start += inc
        rslt.append( start )
    return rslt

n = '22'
a = 4

nval = int(n)
print lbuild( nval, -10, lambda(x,y): (x<10 and x>=0) )
print lbuild( nval, 1, lambda(x,y): x%10 == a-1 )
于 2012-05-15T21:26:36.120 回答