[Python]Processing a String One Character at a Time
来源:互联网 发布:windows ad域的搭建 编辑:程序博客网 时间:2024/06/06 18:31
Problem
You want to process a string one character at a time.
Solution
You can build a list whose items are the string's characters(meaning that the items are strings, each of length of onePython doesn't have a special type for "characters" as distinct from stirngs). Just call the built-in list, with the string as its argument:
You may not even need to build the list, since you can loop directly on the string with a for statement:
do_something_with(c)
or in the for clause of a list comprehension:
or, with exactly the same effects as this list comprehension, you can call a function on each character with the map built-in function:
Discussion
In Python, characters are just strings of length one. You can loop over a string to access each of its characters, one by one. You can use map for much the same purpose, as long as what you need to do with each character is call a function on it. Finally, you can call the built-in type list to obtain a list of the length-one substrings of the string(i.e., the string's characters). If what you want is a set whose elements are the string's characters, you can call sets.set with the string as the argument(in Python 2.4, you can also call the built-in set in just the same way):
magic_chars = set.Set('abracslfjlsjflksdjf')
poppins_chars = sets.Set('supercalifragilisticexpialsjlfjslkjflksjfljsdf')
print ''.join(magic_chars & popins_chars) #set intersection
acrd
- [Python]Processing a String One Character at a Time
- Recipe 1.1. Processing a String One Character at a Time
- 关于学习one step at a time 的English
- uva 12726 One Friend at a Time bfs + 状态压缩
- Makers at work:Folks Reinventing the World One Object or Idea at a Time
- Windows Mobile Development Making a quality platform one application at a time(zhuan)
- ios error:A view can only be associated with at most one view controller at a time
- 387. First Unique Character in a String [easy] (Python)
- leetcode 387. First Unique Character in a String python
- [leetcode: Python]387.First Unique Character in a String
- Error: "Database 'XXX' is already open and can only have one user at a time"
- vss 2005 提示 Only one database connection at a time is supported的处理
- 效率提升最重要的原则 - Doing one thing at a time
- 请求权限调不起来,Can request only one set of permissions at a time
- III-one:A puma at large
- iOS使用XIB文件报错:reason: 'A view can only be associated with at most one view controller at a time!
- one time a day ,change my life
- curl 7.32.0 - 10-at-a-time
- Linux基础 学习笔记
- GDB&&DDD&&Magic C++ &&BVRDE
- 剖析Linux系统启动的后台全过程
- 读取JAR包中配置文件的解决方法
- 低耦合的Struts集成Spring的实例
- [Python]Processing a String One Character at a Time
- 高考作文《细雨闲花》
- Vim功能介绍
- PIVOT & UNPIVOT
- 网络聊天室的实现思想
- sudo详解
- Environment Variables
- 多线程编程——实战篇(二)
- Oracle 总结