Re: [Tutor] rstrip in list? (SOLVED)
There was so many different solutions presented here to me. Thanks to all. By adding '.strip('\n') to the last two lines below, it came out: Sorted List ['102', '231', '463', '487', '555', '961'] for line in file.readlines(): print line.strip('\n'), mylist.append(line.strip('\n')) Further work and studying needed here. LOL. Ken Steven D'Aprano wrote: On Wed, 10 Feb 2010 02:28:43 am Ken G. wrote: I printed out some random numbers to a list and use 'print mylist' and they came out like this: ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n'] I was using 'print mylist.rstrip()' to strip off the '\n' but kept getting an error of : AttributeError: 'list' object has no attribute 'rstrip' You have to apply rstrip to each item in the list, not the list itself. Here are two ways to do it: #1: modify the list in a for-loop for i, item in enumerate(mylist): mylist[i] = item.rstrip() #2: make a new list with a list comprehension mylist = [item.rstrip() for item in mylist] Of the two, I prefer the second. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rstrip in list?
On Tue, Feb 9, 2010 at 11:09 AM, Ken G. wrote: > > Kent Johnson wrote: > > On Tue, Feb 9, 2010 at 10:28 AM, Ken G. 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. If you show the code for this perhaps we can figure out where the newlines are coming from. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rstrip in list?
-Original Message- From: tutor-bounces+bermanrl=cfl.rr@python.org [mailto:tutor-bounces+bermanrl=cfl.rr@python.org] On Behalf Of Ken G. Sent: Tuesday, February 09, 2010 10:29 AM To: tutor@python.org Subject: [Tutor] rstrip in list? I printed out some random numbers to a list and use 'print mylist' and they came out like this: ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n'] 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? TIA, Ken In [14]: mylist Out[14]: ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n'] In [15]: for item in mylist: : print item.strip('\n') : : 102 231 463 487 555 961 I get the impression you can strip something from the elements in the list, but that it balks at a change to the entire list in a single statement? Robert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rstrip in list?
Ken G. wrote: I printed out some random numbers to a list and use 'print mylist' and they came out like this: ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n'] I was using 'print mylist.rstrip()' to strip off the '\n' but kept getting an error of : AttributeError: 'list' object has no attribute 'rstrip' rstrip is a string method, not a list method. You must apply it to each element in the list. One way is: print [item.rstrip() for iten in mylist] -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rstrip in list?
Kent Johnson wrote: On Tue, Feb 9, 2010 at 10:28 AM, Ken G. 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rstrip in list?
On Tuesday 09 February 2010 16:28:43 Ken G. wrote: > ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n'] > > I was using 'print mylist.rstrip()' to strip off the '\n' > > but kept getting an error of : > > AttributeError: 'list' object has no attribute 'rstrip' A string has attribute rstrip not a list. You will need to loop over the list and update each item of the list with rstrip(). This, mylist = [s.rstrip('\n') for s in mylist], should do it. It might be that before making the list you did rstrip() on the individual strings..? Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rstrip in list?
On Wed, 10 Feb 2010 02:28:43 am Ken G. wrote: > I printed out some random numbers to a list and use 'print mylist' > and they came out like this: > > ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n'] > > I was using 'print mylist.rstrip()' to strip off the '\n' > > but kept getting an error of : > > AttributeError: 'list' object has no attribute 'rstrip' You have to apply rstrip to each item in the list, not the list itself. Here are two ways to do it: #1: modify the list in a for-loop for i, item in enumerate(mylist): mylist[i] = item.rstrip() #2: make a new list with a list comprehension mylist = [item.rstrip() for item in mylist] Of the two, I prefer the second. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rstrip in list?
On Tue, Feb 9, 2010 at 10:28 AM, Ken G. wrote: > I printed out some random numbers to a list 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 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor