[Python]Processing a String One Character at a Time
2007-06-28 16:33
381 查看
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:
thelist = list(thestring)
You may not even need to build the list, since you can loop directly on the string with a for statement:
for c in thestring:
do_something_with(c)
or in the for clause of a list comprehension:
results = [do_someting_with(c) for c in thestring]
or, with exactly the same effects as this list comprehension, you can call a function on each character with the map built-in function:
results = map(do_something, thestring)
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):
import sets
magic_chars = set.Set('abracslfjlsjflksdjf')
poppins_chars = sets.Set('supercalifragilisticexpialsjlfjslkjflksjfljsdf')
print ''.join(magic_chars & popins_chars) #set intersection
acrd
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:
thelist = list(thestring)
You may not even need to build the list, since you can loop directly on the string with a for statement:
for c in thestring:
do_something_with(c)
or in the for clause of a list comprehension:
results = [do_someting_with(c) for c in thestring]
or, with exactly the same effects as this list comprehension, you can call a function on each character with the map built-in function:
results = map(do_something, thestring)
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):
import sets
magic_chars = set.Set('abracslfjlsjflksdjf')
poppins_chars = sets.Set('supercalifragilisticexpialsjlfjslkjflksjfljsdf')
print ''.join(magic_chars & popins_chars) #set intersection
acrd
相关文章推荐
- Recipe 1.1. Processing a String One Character at a Time(Python Cookbook)
- Recipe 1.8. Processing a String One Character at a Time
- Recipe 1.9. Processing a String One Word at a Time
- 【Python】python之Character string
- python time, datetime, string, timestamp相互转换
- net.sf.json.JSONException: Unterminated string at character 2101
- Only one database connection at a time is supported
- [77] The pursuit of Happiness-Part 1-One Day at a Time
- Windows Mobile Development Making a quality platform one application at a time(zhuan)
- 效率提升最重要的原则 - Doing one thing at a time
- 转:Move all SQL Server system databases at one time
- 【python】time,datetime,string相互转换
- Python ctypes.string_at
- 效率提升最重要的原则 - Doing one thing at a time
- python之Character string
- python之Character string
- python之Character string(实例讲解)
- vss 2005 提示 Only one database connection at a time is supported的处理
- Makers at work:Folks Reinventing the World One Object or Idea at a Time
- JSON转换出现错误:net.sf.json.JSONException: Unterminated string at character 38 of