Stringified list back to list of ints

Calvin Spealman ironfroggy at socialserve.com
Wed Dec 12 10:38:56 EST 2007


I still hold my vote that if you need to reverse the  
"stringification" of a list, you shouldn't have stringified the list  
and lost hold of the original list in the first place. That is the  
solution above all others.

On Dec 12, 2007, at 10:26 AM, Paul McGuire wrote:

> On Dec 12, 7:25 am, Lee Capps <lca... at cteresource.org> wrote:
>> Regular expressions might be a good way to handle this.
>>
>> import re
>>
>> s = '[16, 16, 2, 16, 2, 16, 8, 16]'
>> get_numbers = re.compile('\d\d*').findall
>>
>> numbers = [int(x) for x in get_numbers(s)]
>>
>
> Isn't '\d\d*' the same as '\d+' ?
>
> And why would you invoke re's when str.split(',') (after stripping
> leading and trailing []'s) does the job so well?
>
> numbers = map(int, s.strip('[]').split(','))
>
> Or if map is not to your liking:
>
> numbers = [int(x) for x in s.strip('[]').split(',')]
>
> -- Paul
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list