找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1400|回复: 1

[求助] python递归如何将元组内的可迭代数据都拆分开

3

主题

3

帖子

3

积分

贫民

积分
3
qiu0312108 发表于 2019-1-24 19:54:23 | 显示全部楼层 |阅读模式
python语言,使用递归方式如何将 ((1, 2, 3), [4, 5, 6], 'asdqwe', 854) 转成 [1, 2, 3, 4, 5, 6, 'a', 's', 'd', 'q', 'w', 'e', 854]。

RT
回复

使用道具 举报

0

主题

4

帖子

4

积分

贫民

积分
4
qingc 发表于 2019-1-25 16:26:22 | 显示全部楼层
  1. # -*-coding:utf-8 -*-

  2. iList = []

  3. def getItemToList(t):
  4.     nLen = len(t)
  5.     for item in range(nLen):
  6.         if isinstance(t[item], (list, tuple, str)) and len(t[item]) > 1:
  7.             getItemToList(t[item])
  8.         else:
  9.             iList.append(t[item])

  10. def main():
  11.     t = ((1, 2, 3), [4, 5, 6], 'asdqwe', 854)
  12.     getItemToList(t)
  13.     print iList

  14. if __name__ == "__main__":
  15.     main()
复制代码
回复 支持 反对

使用道具 举报

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

本版积分规则

快速回复 返回顶部 返回列表