Python 自用代码(递归)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 自用代码(递归)相关的知识,希望对你有一定的参考价值。

将字符串‘ISO 3408-1-2006,MOD  ISO 3408-2-1991,MOD  ISO 3408-3-2006,MOD’存为:

[{‘code‘: ‘ISO 3408-1-2006‘, ‘type‘: ‘MOD‘}, {‘code‘: ‘ISO 3408-2-1991‘, ‘type‘: ‘MOD‘}, {‘code‘: ‘ISO 3408-3-2006‘, ‘type‘: ‘MOD‘}]

#coding=utf-8

s = ISO 3408-1-2006,MOD  ISO 3408-2-1991,MOD  ISO 3408-3-2006,MOD

def func(string, ls):
    dc = {}
    loc = string.find(,)
    if loc==-1:
        return ls
    else:
        dc["code"] = string[:loc].strip()
        dc["type"] = string[loc+1:loc+4]
        ls.append(dc)
        return func(string[loc+4:],ls)

if __name__ == __main__:
    ls = []
    z = func(s, ls)
    print z

 

以上是关于Python 自用代码(递归)的主要内容,如果未能解决你的问题,请参考以下文章