473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Printing with interspersed element

Hi all,

I guess this is a recurring issue for someone who doesn't really know
the python lib inside out. There must be a simple way to do this.
I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
print method for them print_obj(). Now I want to print them
intersepersed by an element.
If I print [x1, x2, x3] interspersed by the element 10:
x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()

Now, the question is, what's the best way to do this?

I guess I could do this recursively.
def print(el, lst):
if len(lst) == 0:
return
elif len(lst) == 1:
lst[0].print_obj()
else:
lst[0].print_obj()
print el,
print(el, lst[1:])

Now, some considerations. This seems cumbersome (it may have errors
has I have not tested and was written directly to the mail, but the
idea is clear). From what I know lst[1:] creates a copy of lst without
the first element which is really not good memory-wise.
So, what would be the python way to do it?

Cheers,

--
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm
Oct 30 '08 #1
8 1428
On 2008-10-30, Paulo J. Matos <po**@ecs.soton.ac.ukwrote:
Hi all,

I guess this is a recurring issue for someone who doesn't really know
the python lib inside out. There must be a simple way to do this.
I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
print method for them print_obj(). Now I want to print them
intersepersed by an element.
If I print [x1, x2, x3] interspersed by the element 10:
x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()
>>','.join([str(i) for i in [1,2,3,4]])
'1,2,3,4'
>>','.join([i.__repr__() for i in [1,2,3,4]])
'1,2,3,4'
>>','.join([str(i) for i in [1]])
'1'

--
Grant Edwards grante Yow! What I want to find
at out is -- do parrots know
visi.com much about Astro-Turf?
Oct 30 '08 #2
On Oct 30, 8:07*pm, "Paulo J. Matos" <p...@ecs.soton.ac.ukwrote:
Hi all,

I guess this is a recurring issue for someone who doesn't really know
the python lib inside out. There must be a simple way to do this.
I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
print method for them print_obj(). Now I want to print them
intersepersed by an element.
If I print [x1, x2, x3] interspersed by the element 10:
x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()

Now, the question is, what's the best way to do this?
Defining a print_obj() method is probably a bad idea. What if you
want to print to a file for example? Instead you can define a
__str__() method for your objects and then use the join() method of
strings like this:

print ' 10 '.join(str(x) for x in lst)

HTH

--
Arnaud

Oct 30 '08 #3
On Thu, Oct 30, 2008 at 8:42 PM, Arnaud Delobelle
<ar*****@googlemail.comwrote:
On Oct 30, 8:07 pm, "Paulo J. Matos" <p...@ecs.soton.ac.ukwrote:
>Hi all,

I guess this is a recurring issue for someone who doesn't really know
the python lib inside out. There must be a simple way to do this.
I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
print method for them print_obj(). Now I want to print them
intersepersed by an element.
If I print [x1, x2, x3] interspersed by the element 10:
x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()

Now, the question is, what's the best way to do this?

Defining a print_obj() method is probably a bad idea. What if you
want to print to a file for example? Instead you can define a
__str__() method for your objects and then use the join() method of
strings like this:

print ' 10 '.join(str(x) for x in lst)
Thanks for the tip but that has an issue when dealing with potentially
millions of objects. You are creating a string in memory to then dump
to a file [or screen] while you could dump to the file [or screen] as
you go through the original string. Right?
HTH

--
Arnaud

--
http://mail.python.org/mailman/listinfo/python-list


--
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm
Oct 30 '08 #4

On 30 Oct 2008, at 21:10, Paulo J. Matos wrote:
On Thu, Oct 30, 2008 at 8:42 PM, Arnaud Delobelle
<ar*****@googlemail.comwrote:
>On Oct 30, 8:07 pm, "Paulo J. Matos" <p...@ecs.soton.ac.ukwrote:
>>Hi all,

I guess this is a recurring issue for someone who doesn't really
know
the python lib inside out. There must be a simple way to do this.
I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
print method for them print_obj(). Now I want to print them
intersepersed by an element.
If I print [x1, x2, x3] interspersed by the element 10:
x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()

Now, the question is, what's the best way to do this?

Defining a print_obj() method is probably a bad idea. What if you
want to print to a file for example? Instead you can define a
__str__() method for your objects and then use the join() method of
strings like this:

print ' 10 '.join(str(x) for x in lst)

Thanks for the tip but that has an issue when dealing with potentially
millions of objects. You are creating a string in memory to then dump
to a file [or screen] while you could dump to the file [or screen] as
you go through the original string. Right?
Why would you want to print millions of objects on the screen?

As for writing to a file, a million objects will probably mean a few
tens of million bytes which is not that much. Your proposed method
would not work as the python call stack would explode first. Here is
one that may meet your approval (it still requires a __str__ method on
your objects but you can adapt it easily):

def print_with_sep(sep, iterable, file=sys.stdout):
iterator = iter(iterable)
try:
file.write(str(iterator.next()))
for item in iterator:
file.write(sep)
file.write(str(item))
except StopIteration:
pass

# Use like this:
>>print_with_sep(' 10 ', [obj1, obj2, obj3])
--
Arnaud

Oct 30 '08 #5
On 2008-10-30, Paulo J. Matos <po******@gmail.comwrote:
On Thu, Oct 30, 2008 at 8:42 PM, Arnaud Delobelle
<ar*****@googlemail.comwrote:
>On Oct 30, 8:07 pm, "Paulo J. Matos" <p...@ecs.soton.ac.ukwrote:
>>Hi all,

I guess this is a recurring issue for someone who doesn't really know
the python lib inside out. There must be a simple way to do this.
I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
print method for them print_obj(). Now I want to print them
intersepersed by an element.
If I print [x1, x2, x3] interspersed by the element 10:
x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()

Now, the question is, what's the best way to do this?

Defining a print_obj() method is probably a bad idea. What if you
want to print to a file for example? Instead you can define a
__str__() method for your objects and then use the join() method of
strings like this:

print ' 10 '.join(str(x) for x in lst)

Thanks for the tip but that has an issue when dealing with potentially
millions of objects. You are creating a string in memory to then dump
to a file [or screen] while you could dump to the file [or screen] as
you go through the original string. Right?
If you want to do it "on the fly", then try something like this:

iter = [1,2,3,4,5].__iter__()
sys.stdout.write(str(iter.next()))
for n in iter:
sys.stdout.write(',' +str(n))

--
Grant Edwards grante Yow! The SAME WAVE keeps
at coming in and COLLAPSING
visi.com like a rayon MUU-MUU ...
Oct 30 '08 #6
On Oct 30, 2:10*pm, "Paulo J. Matos" <pocma...@gmail.comwrote:
On Thu, Oct 30, 2008 at 8:42 PM, Arnaud Delobelle

<arno...@googlemail.comwrote:
On Oct 30, 8:07 pm, "Paulo J. Matos" <p...@ecs.soton.ac.ukwrote:
Hi all,
I guess this is a recurring issue for someone who doesn't really know
the python lib inside out. There must be a simple way to do this.
I have a list of objects [x1, x2, x3, ..., xn] and I have defined a
print method for them print_obj(). Now I want to print them
intersepersed by an element.
If I print [x1, x2, x3] interspersed by the element 10:
x1.print_obj() 10 x2.print_obj() 10 x3.print_obj()
Now, the question is, what's the best way to do this?
Defining a print_obj() method is probably a bad idea. *What if you
want to print to a file for example? *Instead you can define a
__str__() method for your objects and then use the join() method of
strings like this:
print ' 10 '.join(str(x) for x in lst)

Thanks for the tip but that has an issue when dealing with potentially
millions of objects. You are creating a string in memory to then dump
to a file [or screen] while you could dump to the file [or screen] as
you go through the original string. Right?
Then I hope you are using stackless, because you are going to stack
overflow _way_ before you recurse 1 million times.

def print_list(seq, sep=','):
seq = iter(seq)
print seq.next(),
for item in seq:
print sep,
print item,
print

Matt
Oct 31 '08 #7
On Thu, 30 Oct 2008 16:40:17 -0500, Grant Edwards wrote:
If you want to do it "on the fly", then try something like this:

iter = [1,2,3,4,5].__iter__()
sys.stdout.write(str(iter.next()))
for n in iter:
sys.stdout.write(',' +str(n))
Maybe without shadowing the built in `iter()` and without calling the
"magic method" directly:

iterator = iter([1, 2, 3, 4, 5])

Ciao,
Marc 'BlackJack' Rintsch
Oct 31 '08 #8
At 2008-10-30T21:10:09Z, "Paulo J. Matos" <po******@gmail.comwrites:
Thanks for the tip but that has an issue when dealing with potentially
millions of objects. You are creating a string in memory to then dump
to a file [or screen] while you could dump to the file [or screen] as
you go through the original string. Right?
How about:

def alternator(lst, sep):
for index, item in enumerate(lst):
if index:
yield sep
yield item

for item in alternator(list_of_objects, 10):
print item,

--
Kirk Strauser
The Day Companies
Nov 6 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim & Joanne Collins | last post by:
Craig, Thank you very much for your response. I've interspersed my questions in your reply. If you have answers to my question I would be extremely gratefull for your assistance! Thanks.
5
by: Mark Preston | last post by:
Admission first - I don't actually have a problem here but have noticed that a lot of people have been asking similar questions and getting very varied answers. What I've done is to sort of...
9
by: Colin McGuire | last post by:
Hi, I have an report in Microsoft Access and it displays everything in the table. One column called "DECISION" in the table has either 1,2, or 3 in it. On my report it displays 1, 2, or 3. I want...
8
by: Steve Macleod | last post by:
Hi, I was wondering if anyone had a solution for printing HTML elements (especially style elements). I do not wish to make any changes to the page, other than in the <css media="print"block. I can...
8
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web...
14
by: abhishekkarnik | last post by:
Hi, I am trying to read an exe file and print it out character by character in hexadecimal format. The file goes something like this in hexadecimal 0x4d 0x5a 0x90 0x00 0x03 .... so on When I...
3
by: William Chang | last post by:
Is the different behavior between __repr__ and __str__ intentional when it comes to printing lists? Basically I want to print out a list with elements of my own class, but when I overwrite __str__,...
2
by: Latina | last post by:
Hi, Can some one help to figure out why is only printing '{ }' and not the values the user is entering? Here is part of my code: void IntegerSet::setString() { cout<<"{"; for(element=0;...
8
by: Latina | last post by:
Hi, I am doing a program of overloaded methods. I want to get what is the union of the user set and S set and the user set and S1 set. I try it but it is not working. Can some one help me please?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.