Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   I have a list... (http://www.velocityreviews.com/forums/t319046-i-have-a-list.html)

 Damir Hakimov 07-01-2003 07:39 AM

I have a list...

Hi, All!

say, i have a function:

def f(*b):
print b
return

then i do:
f(3,4,5)
(3, 4, 5)

but i have list f=(3,4,5)
f(l)
((3, 4, 5),)

how can i call f function to result
f(???(b))
(3, 4, 5)

Thanks!

 Albert Hofkamp 07-01-2003 07:55 AM

Re: I have a list...

On Tue, 1 Jul 2003 11:39:15 +0400, Damir Hakimov <agg@astranet.ru> wrote:
> Hi, All!
>
> say, i have a function:
>
> def f(*b):
> print b
> return
>
> then i do:
> f(3,4,5)
> (3, 4, 5)
>
> but i have list f=(3,4,5)
> f(l)
> ((3, 4, 5),)
>
> how can i call f function to result
> f(???(b))
> (3, 4, 5)

You mean apply(f,(3,4,5)) ?

Albert
--
Unlike popular belief, the .doc format is not an open publically available format.

 user@domain.invalid 07-01-2003 07:57 AM

Re: I have a list...

Damir Hakimov wrote:
> Hi, All!
>
> say, i have a function:
>
> def f(*b):
> print b
> return
>
> then i do:
> f(3,4,5)
> (3, 4, 5)
>
> but i have list f=(3,4,5)
> f(l)
> ((3, 4, 5),)
>
> how can i call f function to result
> f(???(b))
> (3, 4, 5)
>
> Thanks!
>
>
>

You can use the keyword 'type' to check the type of your arguments
and return the appropriate 'format' according to the their types

Regards

Salvatore

 Dialtone 07-01-2003 07:58 AM

Re: I have a list...

"Damir Hakimov" <agg@astranet.ru> writes:

> say, i have a function:
>
> def f(*b):
> print b
> return
>
> then i do:
> f(3,4,5)
> (3, 4, 5)

This is not a list but a tuple.

> but i have list f=(3,4,5)
> f(l)
> ((3, 4, 5),)

The standard way to represent a tuple with one element is to put a coma
after that element like ("donald",)

> how can i call f function to result
> f(???(b))
> (3, 4, 5)

If you want this you should use a list which has square brackets [].

But the arguments passed with *b are incapsuleted into a tuple so you should
print something like this:

>>> def f(*b):

.... print b[0]

>>> f([1,2,3])

[1, 2, 3]

--
try: troll.uses(Brain)
except TypeError, data:
troll.plonk()
Linux User #310274, Debian Sid Proud User

 Duncan Booth 07-01-2003 08:27 AM

Re: I have a list...

"Damir Hakimov" <agg@astranet.ru> wrote in
news:20030701113915.226ae9bd.agg@astranet.ru:

> Hi, All!
>
> say, i have a function:
>
> def f(*b):
> print b
> return
>
> then i do:
> f(3,4,5)
> (3, 4, 5)
>
> but i have list f=(3,4,5)
> f(l)
> ((3, 4, 5),)
>
> how can i call f function to result
> f(???(b))
> (3, 4, 5)
>

I'm not sure any of the other responses actually answered the question,
which I think was meant to be, given a tuple l=3,4,5 how do you pass that
tuple to the function f so that b simply gets the tuple. The answer is that
you try:

>>> f(*l)

(3,4,5)

If you (or your users) really can't upgrade you should use 'apply'.

--
Duncan Booth duncan@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

 Aahz 07-01-2003 06:31 PM

Re: I have a list...

In article <20030701113915.226ae9bd.agg@astranet.ru>,
Damir Hakimov <agg@astranet.ru> wrote:
>
> [...]

....it never will be missed.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

Apologies to G&S and everyone reading this.

 Bengt Richter 07-01-2003 11:48 PM

Re: I have a list...

On Tue, 1 Jul 2003 11:39:15 +0400, "Damir Hakimov" <agg@astranet.ru> wrote:

>Hi, All!
>
>say, i have a function:
>
>def f(*b):
> print b
> return
>
>then i do:
>f(3,4,5)
>(3, 4, 5)
>
>but i have list f=(3,4,5)
>f(l)
>((3, 4, 5),)
>
>how can i call f function to result
>f(???(b))
>(3, 4, 5)
>

Is this what you are looking for? :

>>> def f(*b):

... print b
...
>>> tup = (1,2,3)
>>> f(tup)

((1, 2, 3),)

tup was single arg, but:

>>> f(*tup)

(1, 2, 3)

tup got unpacked to make args

>>> L = [4,5,6]
>>> f(L)

([4, 5, 6],)

L was single arg, but:

>>> f(*L)

(4, 5, 6)

L got unpacked similarly, but note that args become tuple b, not a list.

Regards,
Bengt Richter

 All times are GMT. The time now is 06:39 AM.