Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 9th, 2004, 01:49 PM
Arteum Arteum is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 4 Arteum User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How to remove spaces from Lists?

Hello,

If I type [1,2] into Python, it'll return [1, 2].

How to make it return [1,2], without that space?

I need to convert my results into strings (for example, str([1,2])), but I don't want extra spaces. Of course, I have a small function that may delete the spaces, but when my result is a very-very long list, and it is then converted to a string, the functoion loses a lot of time just trying to strip the string from the unnecessary spaces!

I'd like to somehow generate the List without spaces from the very beginning.

Does anybody know an answer to this?

Thank you.

Arteum

Reply With Quote
  #2  
Old June 9th, 2004, 02:07 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
This is one way:
Code:
>>> seq = [1, 2, 3]
>>> print "[%s]" % ",".join([str(i) for i in seq])
'[1,2,3]'

This is another way:
Code:
>>> seq = [1, 2, 3]
>>> print str(seq).replace(" ", "")
'[1,2,3]'

Last edited by percivall : June 9th, 2004 at 02:12 PM.

Reply With Quote
  #3  
Old June 9th, 2004, 02:19 PM
Arteum Arteum is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 4 Arteum User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks, percivall,

That may help. My function that removes spaces, does so selectively -- that is, removing them from the lists and leavig them in some other places. I wonder if using either of the two lines you're suggesting will result in a faster overall evaluation.

But I am still intrigued if it is possible to redefine Python standard output somehow, so that no special operations are needed to remove spaces in Lists.

Arteum

Reply With Quote
  #4  
Old June 9th, 2004, 04:10 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
I'm not sure if you understand or not that there're actually no spaces in a list. There are spaces in the representation of a list. When you give the list to str() or to repr() those functions call the __str__ respectively __repr__ methods of the list object. What those two methods do is equivalent to:
Code:
", ".join([str(item) for item in seq])
You can easily change this by deriving your own class from list and overriding the __str__ and/or __repr__ methods.
Code:
class mylist(list):
    def __str__(self):
        return "[%s]" % ",".join([str(item) for item in self])
    __repr__ = __str__

But when you work with a list the representation of the list has nothing to do with the list.

Also, FYI, the representation you see when you enter [1, 2] in the Python interpreter is the same as given by repr(). When you print a list the representation given is the same as given by str().

Last edited by percivall : June 9th, 2004 at 04:17 PM.

Reply With Quote
  #5  
Old June 9th, 2004, 07:17 PM
Arteum Arteum is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 4 Arteum User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you. That clarified everything.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to remove spaces from Lists?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT