python中怎么撤销_在python中取消列表

1586010002-jmsa.png

Problem:

Write a function named "unflatten" which takes a list as an argument

and constructs a nested list.

The format of the argument list is as follows:

An integer item indicates a start of a nested list Non-integer items

will be the content of the nested list For instance,

[2, 'a', 3, 'b', 'c', 'd'] is converted to ['a', ['b', 'c', 'd']] The

first number, 2, indicates that the upper list will contain 2 items.

'a' is the first item of this upper list. The number 3 indicates a

start of a new sub-list which contains 3 items.

Sample Run:

>>> unflatten([2, 'x', 'y'])

['x', 'y']

>>> unflatten([ 3, "a", "b", 3, "t", "y", "u" ])

['a', 'b', ['t', 'y', 'u']]

>>> unflatten([ 4, "a", "b", 3, "c", "d", 2, "x", "y", 2, "w" , 3, "t", "y", "u" ])

['a', 'b', ['c', 'd', ['x', 'y']], ['w', ['t', 'y', 'u']]]

I have done a simple recursion. Here is my code:

def unflatten(LIST):

if not len(LIST):

return []

elif isinstance(LIST[0], int):

return [unflatten(LIST[1:])]

else:

return [LIST[0]] + unflatten(LIST[1:])

>>> unflatten([ 4, "a", "b", 3, "c", "d", 2, "x", "y", 2, "w" , 3, "t", "y", "u" ])

[['a', 'b', ['c', 'd', ['x', 'y', ['w', ['t', 'y', 'u']]]]]]

Now as you can see, lengths of lists are not controlled in my basic recursion, so it simply ends all the lists at the end.

I don't know how I can track the lengths recursively, or maybe iteratively. I'd be glad if you suggest a way doing this without importing any modules.

解决方案

One way to keep track of the position is by returning it. In the code below I use a helper function that returns the partially-built unflattened list as well as the current index in the flattened list.

def unflatten(l):

def helper(l, start):

if isinstance(l[start], int):

ret = []

start += 1

for _ in range(l[start - 1]):

sub, start = helper(l, start)

ret.append(sub)

return ret, start

else:

return l[start], start + 1

return helper(l, 0)[0]

print unflatten([2, 'x', 'y'])

print unflatten([ 3, "a", "b", 3, "t", "y", "u" ])

print unflatten([ 4, "a", "b", 3, "c", "d", 2, "x", "y", 2, "w" , 3, "t", "y", "u" ])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值