[Tutor] rstrip in list?

Ken G. beachkid at insightbb.com
Tue Feb 9 17:09:28 CET 2010


Kent Johnson wrote:
> On Tue, Feb 9, 2010 at 10:28 AM, Ken G. <beachkid at insightbb.com> wrote:
>   
>> I printed out some random numbers to a datafile and use 'print mylist' and
>> they came out like this:
>>
>> ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n']
>>     
>
> How are you generating this list? You should be able to create it
> without the \n. That would be better than stripping them out.
>   
I inputting some random numbers into a database and then created a list
and appended the list as it read the database.
>   
>> I was using 'print mylist.rstrip()' to strip off the '\n'
>>
>> but kept getting an error of :
>>
>> AttributeError: 'list' object has no attribute 'rstrip'
>>
>> My memory must be hazy but I thought I had it working several months ago.
>>
>> Any idea or suggestion?
>>     
>
> Use a list comprehension or map():
>
> In [1]: l = ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n']
>
> In [2]: [ i.rstrip() for i in l ]
> Out[2]: ['102', '231', '463', '487', '555', '961']
>
> In [3]: map(str.rstrip, l)
> Out[3]: ['102', '231', '463', '487', '555', '961']
>
> Kent
>   

My database file has numbers of the same exact length that need to be 
sorted.  I am using
'mylist.sort()' as one of the command.  Actually, I will be using 
'mylist.reverse' after
that command.  I am still in a learning mode.  Thanks.

Ken

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100209/47376a54/attachment.htm>


More information about the Tutor mailing list