list comprehension for splitting strings into pairs
Steven Bethard
steven.bethard at gmail.com
Tue Oct 12 19:47:46 EDT 2004
Steven Bethard <steven.bethard <at> gmail.com> writes:
> >>> splits = [':' in item and item.split(':', 1) or [item, None]
> ... for item in lst]
> >>> splits = [[int(i), p is not None and int(p) or p]
> ... for i, p in splits]
> >>> splits
> [[1, None], [1, 2], [3, None], [-1, 4]]
So I realized that this doesn't quite work if the second item in a split is 0 -
- you'll end up with None instead of 0.
Steve
More information about the Python-list
mailing list