TypeError: 'module object is not callable'

  • Thread starter Bruno Desthuilliers
  • Start date
C

cjt22

Thanks guys. Changing to how Python does things has a lot of geting
used to!
Do any of you have any ideas on the best way to do the following
problem:
Each loop I perform, I get a new list of Strings.
I then want to print these lists as columns adjacent to each other
starting with the first
created list in the first column and last created list in the final
column.
If you need any more information, just let me know!
Cheers

If I understand correctly what you may want is:
l = ['1', '2', '3', '4']

you can do:

assuming "\t" as the delimiter

or,

.... print i, '\t' , # note the trailing ","

If this isnotwhat you want, post an example.

Btw, Please post new issues in a separate thread.

Cheers,
--

I think that is very similar to what I want to do.
Say I had lists a = ["1" , "2", "3"] b = ["4", "5", "6"] c = ["7",
"8", "9"]
Stored in another list d = [a,b,c]
I want the printed output from d to be of the form:
1 4 7
2 5 8
3 6 9
From what I am aware, there is no table module to do this. The '\t'
operator looks like it can allow this,
I am playing with it at the moment, although going for my lunch break
now!
 
A

Amit Khemka

Thanks guys. Changing to how Python does things has a lot of geting
used to!
Do any of you have any ideas on the best way to do the following
problem:
Each loop I perform, I get a new list of Strings.
I then want to print these lists as columns adjacent to each other
starting with the first
created list in the first column and last created list in the final
column.
If you need any more information, just let me know!
Cheers

If I understand correctly what you may want is:
l = ['1', '2', '3', '4']

you can do:
print "\t".join(l) # lookup join method in stringmodule,

assuming "\t" as the delimiter

or,
for i in l:

.... print i, '\t' , # note the trailing ","

If this isnotwhat you want, post an example.

Btw, Please post new issues in a separate thread.

Cheers,
--

I think that is very similar to what I want to do.
Say I had lists a = ["1" , "2", "3"] b = ["4", "5", "6"] c = ["7",
"8", "9"]
Stored in another list d = [a,b,c]
I want the printed output from d to be of the form:
1 4 7
2 5 8
3 6 9
From what I am aware, there is no table module to do this. The '\t'
operator looks like it can allow this,
I am playing with it at the moment, although going for my lunch break
now!

Have a look at function 'zip' or function 'izip' in module "itertools" .
 
D

Duncan Booth

A.T.Hofkamp said:
Each loop I perform, I get a new list of Strings.
I then want to print these lists as columns adjacent to each other
starting with the first
created list in the first column and last created list in the final
column.

Use zip:
x = ['1', '2']
y = ['3', '4']
for row in zip(x,y):
... print ', '.join(row)
...
1, 3
2, 4


zip() constructs a list of rows, like

[('1', '3'), ('2', '4')]

which is then quite easy to print

But watch out if the lists aren't all the same length: zip won't pad out
any sequences, so it may not be exactly what is wanted here:
x = ['1', '2', '3']
y = ['4', '5']
for row in zip(x,y):
print ', '.join(row)


1, 4
2, 5
 
C

cjt22

But watch out if the lists aren't all the same length: zip won't pad out
any sequences, so it maynotbe exactly what is wanted here:
x = ['1', '2', '3']
y = ['4', '5']
for row in zip(x,y):

print ', '.join(row)

1, 4
2, 5

Unfortunately the lists will be of different sizes
By the way I apologise for different questions within the same thread.
When I have another question, I will make a new topic
 
D

Duncan Booth

But watch out if the lists aren't all the same length: zip won't pad out
any sequences, so it maynotbe exactly what is wanted here:
x = ['1', '2', '3']
y = ['4', '5']
for row in zip(x,y):

print ', '.join(row)

1, 4
2, 5

Unfortunately the lists will be of different sizes

In that case use:

from itertools import repeat, chain, izip
def izip_longest(*args, **kwds):
fillvalue = kwds.get('fillvalue')
def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
yield counter() # yields the fillvalue, or raises IndexError
fillers = repeat(fillvalue)
iters = [chain(it, sentinel(), fillers) for it in args]
try:
for tup in izip(*iters):
yield tup
except IndexError:
pass


x = ['1', '2', '3']
y = ['4', '5']
for row in izip_longest(x,y, fillvalue='*'):
print ', '.join(row)


which gives:

1, 4
2, 5
3, *


(izip_longest is in a future version of itertools, but for
now you have to define it yourself).
 
C

cjt22

But watch out if the lists aren't all the same length: zip won't pad out
any sequences, so it maynotbe exactly what is wanted here:
x = ['1', '2', '3']
y = ['4', '5']
for row in zip(x,y):
print ', '.join(row)
1, 4
2, 5
Unfortunately the lists will be of different sizes

In that case use:

from itertools import repeat, chain, izip
def izip_longest(*args, **kwds):
fillvalue = kwds.get('fillvalue')
def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
yield counter() # yields the fillvalue, or raises IndexError
fillers = repeat(fillvalue)
iters = [chain(it, sentinel(), fillers) for it in args]
try:
for tup in izip(*iters):
yield tup
except IndexError:
pass

x = ['1', '2', '3']
y = ['4', '5']
for row in izip_longest(x,y, fillvalue='*'):
print ', '.join(row)

which gives:

1, 4
2, 5
3, *

(izip_longest is in a future version of itertools, but for
now you have to define it yourself).

Thanks guys

I have a list of lists such as
a = ["1" , "2"] b = ["4", "5", "6"] c = ["7",8", "9"]
Stored in another list: d = [a,b,c]

I know this makes me sound very stupid but how would I specify
in the parameter the inner lists without having to write them all out
such as:

for row in izip_longest(d[0], d[1], d[2], fillvalue='*'):
print ', '.join(row)

i.e. How could I do the following if I didn't know how many list of
lists I had.
Sorry this sounds stupid and easy.
Thankyou very much in advance as well, you are all very helpful
indeed.
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
(snip)
Thanks guys

I have a list of lists such as
a = ["1" , "2"] b = ["4", "5", "6"] c = ["7",8", "9"]
Stored in another list: d = [a,b,c]

I know this makes me sound very stupid but how would I specify
in the parameter the inner lists without having to write them all out
such as:

for row in izip_longest(d[0], d[1], d[2], fillvalue='*'):
print ', '.join(row)

i.e. How could I do the following if I didn't know how many list of
lists I had.

for row in izip_longest(*d, fillvalue='*'):
print ', '.join(row)


HTH
 
C

cjt22

(e-mail address removed) a écrit :
(snip)
Thanks guys
I have a list of lists such as
a = ["1" , "2"] b = ["4", "5", "6"] c = ["7",8", "9"]
Stored in another list: d = [a,b,c]
I know this makes me sound very stupid but how would I specify
in the parameter the inner lists without having to write them all out
such as:
for row in izip_longest(d[0], d[1], d[2], fillvalue='*'):
print ', '.join(row)
i.e. How could I do the following if I didn't know how many list of
lists I had.

for row in izip_longest(*d, fillvalue='*'):
print ', '.join(row)

HTH

I thought that but when I tried it I recieved a
"Syntax Error: Invalid Syntax"
with a ^ pointing to fillvalue :S
 
C

cjt22

(e-mail address removed) a écrit :
(snip)
Thanks guys
I have a list of lists such as
a = ["1" , "2"] b = ["4", "5", "6"] c = ["7",8", "9"]
Stored in another list: d = [a,b,c]
I know this makes me sound very stupid but how would I specify
in the parameter the inner lists without having to write them all out
such as:
for row in izip_longest(d[0], d[1], d[2], fillvalue='*'):
print ', '.join(row)
i.e. How could I do the following if I didn't know how many list of
lists I had.

for row in izip_longest(*d, fillvalue='*'):
print ', '.join(row)

HTH

I thought that, but when I tried it, I recieved a
"Syntax error: invalid syntax"
message with ^ pointing to the 'e' of fillvalue
 
D

Duncan Booth

I know this makes me sound very stupid but how would I specify
in the parameter the inner lists without having to write them all out
such as:

for row in izip_longest(d[0], d[1], d[2], fillvalue='*'):
print ', '.join(row)

i.e. How could I do the following if I didn't know how many list of
lists I had.
Sorry this sounds stupid and easy.
Thankyou very much in advance as well, you are all very helpful
indeed.

The variable arguments have to appear last in the call (in the order *x,
**y if you have both):
a = ["1" , "2"]; b = ["4", "5", "6"]; c = ["7", "8", "9"]
d = [a, b, c]
for row in izip_longest(fillvalue='*', *d):
print ', '.join(row)


1, 4, 7
2, 5, 8
*, 6, 9
 
H

Hrvoje Niksic

I thought that but when I tried it I recieved a
"Syntax Error: Invalid Syntax"
with a ^ pointing to fillvalue :S

Python isn't too happy about adding individual keyword arguments after
an explicit argument tuple. Try this instead:

for row in izip_longest(*d, **dict(fillvalue='*')):
print ', '.join(row)
 
M

Miles

Python isn't too happy about adding individual keyword arguments after
an explicit argument tuple. Try this instead:

for row in izip_longest(*d, **dict(fillvalue='*')):
print ', '.join(row)

Or simply:

for row in izip_longest(fillvalue='*', *d):
print ', '.join(row)

-Miles
 
C

cjt22

Thanks guys, I really appreciate it. I have never used google groups
before
and am so impressed with how helpful you all are. It is also lovely
that
none of you mock my little knowledge of Python but just want to
improve it.

I have another question in relation to the izip_longest function (I
persume
this should be within the same topic).
Using this funciton, is there a way to manipulate it so that the
columns can be formated
tabular i.e. perhaps using something such as str(list).rjust(15)
because currently the columns
overlap depending on the strings lengths within each column/list of
lists. i.e. my output is
currently like:

bo, daf, da
pres, ppar, xppc
magnjklep, *, dsa
*, *, nbi

But I want it justified, i.e:

bo , daf, da
pres , ppar, xppc
magnjklep, *, dsa
* , *, nbi

I am struggling to understand how the izip_longest function works
and thus don't really know how it could be manipulated to do the
above.
It would be much apprecited if somoene could also explain how
izip_function
works as I don't like adding code into my programs which I struggle to
understand.
Or perhaps I have to pad out the lists when storing the Strings?

Any help would be much appreciated.
 
A

Amit Khemka

Thanks guys, I really appreciate it. I have never used google groups
before and am so impressed with how helpful you all are. It is also lovely
that none of you mock my little knowledge of Python but just want to
improve it.

And we are proud of it !
I have another question in relation to the izip_longest function (I
persume
this should be within the same topic).
Using this funciton, is there a way to manipulate it so that the
columns can be formated
tabular i.e. perhaps using something such as str(list).rjust(15)
because currently the columns
overlap depending on the strings lengths within each column/list of
lists. i.e. my output is
currently like:

bo, daf, da
pres, ppar, xppc
magnjklep, *, dsa
*, *, nbi

But I want it justified, i.e:

bo , daf, da
pres , ppar, xppc
magnjklep, *, dsa
* , *, nbi

You can format the output while "print"ing the table. Have a look at:

http://www.python.org/doc/current/lib/typesseq-strings.html

example:
for tup in izip_longest(*d, **dict(fillvalue='*')):
print "%15s, %15s, %15s" %tup # for a tuple of length 3, you can
generalize it

I am struggling to understand how the izip_longest function works
and thus don't really know how it could be manipulated to do the
above.
It would be much apprecited if somoene could also explain how
izip_function
works as I don't like adding code into my programs which I struggle to
understand.
Or perhaps I have to pad out the lists when storing the Strings?

Any help would be much appreciated.

This is an example of "generator" functions, to understand what they
are and how they work you can:
1. web-search for "python generators"
2. have a look at "itertools" module, for more generators
 
R

Ryan Ginstrom

On Behalf Of (e-mail address removed)
bo, daf, da
pres, ppar, xppc
magnjklep, *, dsa
*, *, nbi

But I want it justified, i.e:

bo , daf, da
pres , ppar, xppc
magnjklep, *, dsa
* , *, nbi

Once you have a nice rectangular list of lists, you might want to take a
look at my padnums module.

# Usage:
import padnums
import sys

table = [row for row in izip_longest(*d, fillvalue='*')]
padnums.pprint_table(sys.stdout, table)

Code described here, with link to module:
http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/

Regards,
Ryan Ginstrom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,275
Latest member
Michaelachoda

Latest Threads

Top