我想更改列表的表示形式.. 在这段代码之后我扭曲了输出,我试过没有成功 所以,如果有人能帮助我,我会很感激
empty_rlist = None
def make_rlist(first,rest):
return (first, rest)
def first(s):
return s[0]
def rest(s):
return s[1]
def len_rlist(s):
length = 0
while s != empty_rlist:
s, length = rest(s), length + 1
return length
def getitem_rlist(s, i):
while i > 0:
s, i = rest(s), i - 1
return first(s)
def make_mutable_rlist():
contents = empty_rlist
def length():
return len_rlist(contents)
def get_item(ind):
return getitem_rlist(contents, ind)
def push_first(value):
nonlocal contents
contents = make_rlist(value, contents)
def pop_first():
nonlocal contents
f = first(contents)
contents = rest(contents)
return f
def str():
return str(contents)
return {'length':length, 'get_item':get_item, 'push_first':push_first,'pop_first': pop_first, 'str':str }
my_list = make_mutable_rlist()
for x in range(4):
my_list['push_first'](x)
print(my_list['str']())
我需要改变str函数的实现: 这给了我结果
(1, (2, (3, (4, None))))
但我想要的是
[1,2,3,4]