FAQ
I've got a function that takes an arbitrary number of parameters.
(This is the Gnuplot.plot() function.) I don't know until runtime
how many things I want to send to it. So I thought I'd be clever
and do it as
plotdata = ()
for i in range(stuff):
plotdata = plotdata + (function(i),)
Gnuplot.plot(plotdata)

But the plot function is dying due to:
TypeError: bad argument type for built-in operation

I realize plotdata is a tuple. So this is the equivalent of me
calling Gnuplot.plot((0, 1, 2))
Note the double parenthesis... I'm guessing I just need to get
rid of them. Is there a way to convert from a tuple to its
non-parenthesized equivalent?

Only thing I can think of would be to do it using a utility function:
def remove_parens(tuple)
if len(tuple)==1: return tuple[0]
if len(tuple)==2: return tuple[0], tuple[1]
if len(tuple)==3: return tuple[0], tuple[1], tuple[2], tuple[3]
...etc. And I'm not even sure that would work...

Surely there's a better way?

CC's appreciated, but not required.

Damian Menscher
--
-=#| Physics Grad Student & SysAdmin @ U Illinois Urbana-Champaign |#=-
-=#| 488 LLP, 1110 W. Green St, Urbana, IL 61801 Ofc:(217)333-0038 |#=-
-=#| 1429 DCL, Workstation Services Group, CITES Ofc:(217)244-3862 |#=-
-=#| <menscher at uiuc.edu> www.uiuc.edu/~menscher/ Fax:(217)333-9819 |#=-

Search Discussions

  • Sean 'Shaleh' Perry at May 21, 2002 at 6:28 am

    I realize plotdata is a tuple. So this is the equivalent of me
    calling Gnuplot.plot((0, 1, 2))
    Note the double parenthesis... I'm guessing I just need to get
    rid of them. Is there a way to convert from a tuple to its
    non-parenthesized equivalent?
    sounds like you need the apply() builtin.
  • Laura Creighton at May 21, 2002 at 6:34 am
    You want the apply function. In newer Pythons *plotdata will do, since
    * is a synonym for apply, (and **plotdict if you wanted to pass in
    a dictionary of name:value pairs).

    Laura Creighton
  • Erik Max Francis at May 21, 2002 at 6:37 am

    Damian Menscher wrote:

    I've got a function that takes an arbitrary number of parameters.
    (This is the Gnuplot.plot() function.) I don't know until runtime
    how many things I want to send to it. So I thought I'd be clever
    and do it as
    plotdata = ()
    for i in range(stuff):
    plotdata = plotdata + (function(i),)
    Gnuplot.plot(plotdata)

    But the plot function is dying due to:
    TypeError: bad argument type for built-in operation
    If f is a function and args is a tuple of the arguments you want to send
    it, try:

    apply(f, args)

    or just

    f(*args)

    --
    Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
    __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
    / \ Who'd ever think it / Such a squalid little ending
    \__/ The American and Florence, _Chess_
    Church / http://www.alcyone.com/pyos/church/
    A lambda calculus explorer in Python.
  • Damian Menscher at May 21, 2002 at 7:19 am

    Laura Creighton wrote:
    You want the apply function. In newer Pythons *plotdata will do, since
    * is a synonym for apply, (and **plotdict if you wanted to pass in
    a dictionary of name:value pairs).
    Many thanks to all who responded. I especially love this solution,
    since it means only one character change and my program works. ;)

    Damian Menscher
    --
    -=#| Physics Grad Student & SysAdmin @ U Illinois Urbana-Champaign |#=-
    -=#| 488 LLP, 1110 W. Green St, Urbana, IL 61801 Ofc:(217)333-0038 |#=-
    -=#| 1429 DCL, Workstation Services Group, CITES Ofc:(217)244-3862 |#=-
    -=#| <menscher at uiuc.edu> www.uiuc.edu/~menscher/ Fax:(217)333-9819 |#=-

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 21, '02 at 5:59a
activeMay 21, '02 at 7:19a
posts5
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase