rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 learn more   get in touch 

Logo - I Build Search
Feb 19
2010

Join a list of integers in Python digg

Today, I had to pretty print a list of integers for debugging. This does not work:

>>> t = [1, 2, 3, 4]
>>> ' '.join(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: sequence item 0: expected string, int found

So I came up with this:

>>> def concat(x, y): return str(x) + ' ' + str(y)
>>> reduce(concat, t)
'1 2 3 4'

I am sure there is a better way of doing this!

One Response (rss) (trackback)

#1

Alexa.3emyh

February 24th, 2010 at 8:33 pm

without def any function:
>>> t=[1,2,3,4]
>>> ‘ ‘.join([str(y) for y in t])

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Latest Articles

Apr
07

Palindromic sub-sequences in python

This bit of python code returns all palindromic subsequences in the input string.

[Read More]
Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it!

[Read More]

Featured Projects

NLP classes for PHP

NLP classes for PHP

This is an ongoing project to develop a set of classes for Natural Language Processing. Some code would be ported from the NLTK project.

[Read More]

Yahoo Messenger Client for *nix

Yahoo Messenger Client for *nix

Yux is an alternative Yahoo Messenger client for *nix systems that attempts to match the look and feel of the original Windows client.

[Read More]

This page and its contents are copyright © 2010, Pravin Paratey.