我的账户
码尚云教育

云计算企业项目实战营

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

python递归的使用

[复制链接]
云鼎 发表于 2022-9-19 14:24:42 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
很多时候我们在写代码时,可能会用到一些场景,例如,我们想把一些数据通过一些特殊符号分割开来,然后去取其中的一些部分数据,或者完整的数据,
这个时候就用到了一些list和递归的一些小用法,今天小编来写写。
懂我风格的人都知道,废话少说,直接上代码!
  • def getInfo():
  •     myList = []
  •     infoList = "test&my&xiang&test&de"
  •     ealList = []
  •     ptext = infoList
  •     fed = infoList
  •     if infoList.count("&") >= 1:
  •         for j in range(0, len(ptext)):
  •             if ptext[j] == "&":
  •                 myList.append(j)
  •         ealList.append(ptext[0:myList[0]])
  •         for sIndex in range(1, len(myList)):
  •             ptext = diGui(fed, myList, sIndex)
  •             ealList.append(ptext)
  •         ealList.append(fed[myList[-1] + 1:])
  •         print(ealList)  # 打印list
  •     else:
  •         ealList.append(ptext)
  •         print(ealList[0])
  •     print(ealList[0])  # 取出第一个数据
  • def diGui(test, ilist, sx):
  •     getText = test[ilist[sx - 1] + 1:ilist[sx]]
  •     return getText
  • if __name__ == '__main__':
  •     getInfo()

[color=rgb(15, 199, 122) !important]复制代码



运行后,打印出信息为:
['test', 'my', 'xiang', 'test', 'de']
test

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注我们
码尚云云计算培训

客服电话:18710061913

周一至周日: 7:00-24:00

码尚云教育( 陕ICP备2023009934号-2 )

Powered by Discuz! X3.4© 2016-2023 Comsenz Inc.