我可以更改课程中列表的长度吗?

时间:2014-09-18 00:59:45

标签: python list class

如果我定义了一个类" Zillion",我将输入作为

m = Zillion([9,9,9,9,9,9])

然后我应用一个名为" increment"在Zillion类中定义。我希望得到

[1,0,0,0,0,0,0].

问题是:当我[1,0,0,0,0,0,0]时,我可以m.increment。但如果我重复m.increment,结果就会变成

[0,0,0,0,0,1]
[0,0,0,0,0,2]
......

好像我无法改变课堂上的长度。我可以吗?

2 个答案:

答案 0 :(得分:0)

当然可以,您应该意识到,只要您声明可以获得[1,0,0,0,0,0,0]。显然问题不在于获得额外数字,而是保持额外数字。

在没有看到你的代码的情况下,我们实际上无法告诉你它有什么问题(有一定程度的确定性),但是你会发现你可以得到保持更长的列表。代码如下。首先,强制初始化:

class Zillion:
    # Init: simply set to zero.

    def __init__(self):
        self.data = [0]

现在,代码增加一个数字。它同样使用任何体面的小学教授的算法:

    # Increment the number.

    def increment(self):
        # Force 1 to be added initially.

        carry = 1

        # Iterate over digits in reverse.

        for index in range(len(self.data) - 1,-1,-1):
            # Add current carry and set new carry based on result.

            self.data[index] = self.data[index] + carry
            if self.data[index] == 10:
                self.data[index] = 0
                carry = 1
            else:
                carry = 0

        # If carry at end then there was overflow, insert 1 at left.

        if carry == 1:
            self.data.insert(0,1)

然后只是一些测试工具代码,以便您可以看到它的实际效果:

    # Code for debugging.

    def output(self):
        print self.data

# Test code here, show various increments.

if __name__ == "__main__":
    z = Zillion()

    for x in range(8): # -> 8
        z.increment()

    for x in range(13): # -> 21
        z.output()
        z.increment()
    z.output()

    print "==="
    for x in range(999977): # -> 999,998
        z.increment()

    for x in range(13): # -> 1,000,011
        z.output()
        z.increment()
    z.output()

运行该代码可让您确信列表的长度会发生变化并保持正确:

[8]
[9]
[1, 0]
[1, 1]
[1, 2]
[1, 3]
[1, 4]
[1, 5]
[1, 6]
[1, 7]
[1, 8]
[1, 9]
[2, 0]
[2, 1]
===
[9, 9, 9, 9, 9, 8]
[9, 9, 9, 9, 9, 9]
[1, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 1]
[1, 0, 0, 0, 0, 0, 2]
[1, 0, 0, 0, 0, 0, 3]
[1, 0, 0, 0, 0, 0, 4]
[1, 0, 0, 0, 0, 0, 5]
[1, 0, 0, 0, 0, 0, 6]
[1, 0, 0, 0, 0, 0, 7]
[1, 0, 0, 0, 0, 0, 8]
[1, 0, 0, 0, 0, 0, 9]
[1, 0, 0, 0, 0, 1, 0]
[1, 0, 0, 0, 0, 1, 1]

答案 1 :(得分:0)

试试这个:

#!/usr/bin/python

class Zillion():
    def __init__(self, lst):
       self._lst = lst
    def increment(self):
       sum = int(''.join([str(z) for z in self._lst])) + 1
       self._lst = [int(z) for z in list(str(sum))]
    def __str__(self):
       return str(self._lst)

if __name__ == '__main__':
   z = Zillion([9,9,9,9,9,9])
   print 'Initially, z=%s' % z
   for i in range(11):
      z.increment()
      print 'Incremented, list is now = %s' % z


$ python x.py 
Initially, z=[9, 9, 9, 9, 9, 9]
Incremented, list is now = [1, 0, 0, 0, 0, 0, 0]
Incremented, list is now = [1, 0, 0, 0, 0, 0, 1]
Incremented, list is now = [1, 0, 0, 0, 0, 0, 2]
Incremented, list is now = [1, 0, 0, 0, 0, 0, 3]
Incremented, list is now = [1, 0, 0, 0, 0, 0, 4]
Incremented, list is now = [1, 0, 0, 0, 0, 0, 5]
Incremented, list is now = [1, 0, 0, 0, 0, 0, 6]
Incremented, list is now = [1, 0, 0, 0, 0, 0, 7]
Incremented, list is now = [1, 0, 0, 0, 0, 0, 8]
Incremented, list is now = [1, 0, 0, 0, 0, 0, 9]
Incremented, list is now = [1, 0, 0, 0, 0, 1, 0]