001_001 Python字符串一个个遍历
来源:互联网 发布:mac更多快捷键 编辑:程序博客网 时间:2024/05/16 19:04
代码如下:
#coding=utf-8print '中国'#每次处理一个字符串mystr = u'test string中国'#方案一 使用listliststr = list(mystr)for i in liststr: print '1-',i print liststr.__len__()print liststr[12]#方案二 使用forfor i in mystr: print '2-',i #方案三使用列表推导def fun_print(c): print '3-',c res = [fun_print(c) for c in mystr]#方案四使用mapdef fun_printmap(c): print '4-',cresmap = map(fun_printmap,mystr)#otherimport sets #导入集合left = sets.Set('abcabc') right = sets.Set('abcdefabcdef')print leftprint rightprint left & right #交集print left | right #并集print ' '.join(left &right)
打印结果如下:
中国
1- t
1- e
1- s
1- t
1-
1- s
1- t
1- r
1- i
1- n
1- g
1- 中
1- 国
13
国
2- t
2- e
2- s
2- t
2-
2- s
2- t
2- r
2- i
2- n
2- g
2- 中
2- 国
3- t
3- e
3- s
3- t
3-
3- s
3- t
3- r
3- i
3- n
3- g
3- 中
3- 国
4- t
4- e
4- s
4- t
4-
4- s
4- t
4- r
4- i
4- n
4- g
4- 中
4- 国
Set(['a', 'c', 'b'])
Set(['a', 'c', 'b', 'e', 'd', 'f'])
Set(['a', 'c', 'b'])
Set(['a', 'c', 'b', 'e', 'd', 'f'])
a c b
0 0
- 001_001 Python字符串一个个遍历
- Python中 如何将一个字符串分成一个个字符;
- 002_001 Python 读取文件
- 004_001 Python 对象拷贝
- 005_001 Python 对字典排序
- 006_001 Python 温度标示的转换
- 003_001 Python 计算昨天和明天的日期
- python逐个遍历字符串(含汉字)
- 数据结构_001
- 挖坑一个个
- python 遍历所有文件文件夹,并且搜索其中的字符串
- python遍历文件夹找到包含某字符串的文件
- Python 遍历目录找出包含指定字符串的文件夹
- Python itertools 模块 列表元素 字符串 反向遍历合并
- 字符串遍历
- 遍历字符串
- python -- python中的遍历
- 羽毛球记事_001
- shared_ptr
- 【Unity Shaders】Using Textures for Effects——打包和混合textures
- X86&64 calling convention
- Backbone.js 简易入门教程
- ASP.NET基础
- 001_001 Python字符串一个个遍历
- 从C源代码到可执行文件的总体过程框架
- android下Rotate旋转动画实现效果
- SRM 571 D1L2:MagicMolecule,brute force
- nyoj 375
- 安装CENTOS 分区方案
- Debugging Mobile Web Page
- 中国2012大事记
- poj2352之树状数组