Python 我可以更改一个类中列表的长度吗?
假设我定义了一个类“Zillion”,并将输入作为Python 我可以更改一个类中列表的长度吗?,python,list,class,Python,List,Class,假设我定义了一个类“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]。但是如果我重复m.increment,结果会变为 [0,0,0,0,0,1] [0,0,0,0,0,2] ...... 似乎我不能改变课堂上的长度。我可以吗?当然可以,你应该意识到只要你声明你就可以
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]
。但是如果我重复m.increment
,结果会变为
[0,0,0,0,0,1]
[0,0,0,0,0,2]
......
似乎我不能改变课堂上的长度。我可以吗?当然可以,你应该意识到只要你声明你就可以得到
[1,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]
试试这个:
#!/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]
请发布相关代码。您能做什么或不能做什么很大程度上取决于
Zillion.increment
的实现。非常感谢!!但是如果我的输入是从字符串转换过来的。比如m.Zillion=('999 999'),它将给我输出为[9,9,9,9,9,9],并在init函数中完成。然后在[9,9,9,9,9,9]上应用增量法。我还能更改长度吗?@Lily,没有必要告诉我们代码是什么,问我们一个显示代码的问题。事实上,你的列表是1000倍,这意味着除了递增之外还有其他问题。在任何情况下,您都可以更改我显示的长度-无论原始数据是来自硬编码的[0]
还是字符串都不相关。@paxdiabo,谢谢您的解释。我想将我的代码粘贴到那里。但是我正在为我的实验室写一个代码,我的教授说我们最好不要在上交代码之前在网上发布代码。。。所以我很关心…@莉莉,没关系。这只是意味着我们不能很容易地帮助你解决你的具体问题。我怀疑你必须仔细查看我们提供的内容,然后将这些知识应用到你自己的代码中(从教育的角度来看,这是很好的)。@paxdiabo,如果我粘贴我的代码,我解决问题后是否可能删除它?