Python itertools 模块 列表元素 字符串 反向遍历合并
2016-04-29 13:08
1261 查看
存在这样一个list: number = [‘a’,’b’,’c’,’d’]
要求输出 dabc cabd bacd abcd
适用于 列表(不能为数字)和字符串(不能为字母)
输出:
关于迭代器请参考 可爱的 Python:使用 itertools 模块中的组合函数
要求输出 dabc cabd bacd abcd
适用于 列表(不能为数字)和字符串(不能为字母)
#coding:utf-8 ''' FuncName: listtmp.py Desc: itertools chain Date: 2016-04-29 12:00 Author: johnny ''' import itertools # 引入迭代器 number = ['a','b','c','d'] #number = '1234' 若要用数字就使用这个字符串格式,取消上面的列表 listlen = len(number) # 获取长度 for num in xrange(listlen): nums = num-1 tmp1 = "".join(itertools.chain(*number[0:nums])) # "".join(itertools.chain(*number[0:3])) == abc if num == 0 : # 第一个数只需2项 out1 = number[nums]+tmp1 # 最后一项 + 其他项 print out1 else: tmp2 = "".join(itertools.chain(*number[num:listlen])) out2 = number[nums]+tmp1+tmp2 # 遍历项 + 0到遍历项 + 遍历后面的项 print out2
输出:
dabc abcd bacd cabd
关于迭代器请参考 可爱的 Python:使用 itertools 模块中的组合函数
相关文章推荐
- python中使用字符串前面加r
- ubuntu下python环境变量配置以及将模块放入正确位置
- [python]join函数操作字符串
- 史上最快python 异步消息队列zeromq 简介
- 使用Python进行多进程编程
- Python实现包含min函数的栈
- Python爬虫实战:百度贴吧—妈妈吧
- Python二叉搜索树与双向链表转换实现方法
- python中使用fork创建新的进程
- 游戏运维通用python简易脚本
- Python实现简单字典树的方法
- Python编码介绍——encode和decode
- Python自动化运维工具fabric的安装
- python3 + Django 学习之路(一)
- 解决fatal error: Python.h: No such file or directory
- python正则表达式
- python中使用epoll开发服务端程序
- Python的Urllib库的使用(爬虫基础)
- Python中操作符重载用法分析
- 利用百度地图API制作城中心交通时空圈