【问题标题】:Can I change the length of list in a class?我可以更改班级列表的长度吗?
【发布时间】:2014-09-18 00:59:45
【问题描述】:

假设我定义了一个类“Zillion”,我将输入设为

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

然后我应用在 Zillion 类中定义的名为“increment”的方法。我希望得到

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

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

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

似乎我无法更改班级的长度。可以吗?

【问题讨论】:

  • 请贴出相关代码
  • 你能做什么或不能做什么很大程度上取决于Zillion.increment..的实现。

标签: python list class


【解决方案1】:

试试这个:

#!/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]

【讨论】:

    【解决方案2】:

    当然可以,你应该已经意识到,只要你说你可以得到[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]
    

    【讨论】:

    • 非常感谢!!但是,如果我的输入是从字符串转换的。像 m.Zillion = ('999 999 999') 它会给我输出 [9,9,9,9,9,9] 并且它在 init 函数中完成。然后我在 [9,9,9,9,9,9] 上应用增量方法。我还能改变长度吗?
    • @Lily,没有意义告诉我们代码的作用,问一个问题显示我们的代码。您列出的事实是 1000 倍,这意味着存在 其他 问题,而不仅仅是递增。在任何情况下,您可以更改长度,如我所示 - 无论原始数据来自硬编码的 [0] 还是不相关的字符串。
    • @paxdiabo,感谢您的解释。我想将我的代码粘贴到那里。但是我正在为我的实验室编写代码,我的教授说我们最好不要在上交代码之前将代码发布到互联网上......所以我很担心......
    • @Lily,没关系。这只是意味着我们不能很容易地帮助您解决您的具体问题。我怀疑您必须查看我们提供的内容,然后将这些知识应用到您自己的代码中(无论如何从教育的角度来看这很好)。
    • @paxdiabo,如果我粘贴我的代码,我解决问题后是否可以删除它?
    猜你喜欢
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多