Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > I have a list...

Reply
Thread Tools

I have a list...

 
 
Damir Hakimov
Guest
Posts: n/a
 
      07-01-2003
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!



 
Reply With Quote
 
 
 
 
Albert Hofkamp
Guest
Posts: n/a
 
      07-01-2003
On Tue, 1 Jul 2003 11:39:15 +0400, Damir Hakimov <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
user@domain.invalid
Guest
Posts: n/a
 
      07-01-2003
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


 
Reply With Quote
 
Dialtone
Guest
Posts: n/a
 
      07-01-2003
"Damir Hakimov" <(E-Mail Removed)> 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
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      07-01-2003
"Damir Hakimov" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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 that doesn't work, then you upgrade to a more recent version of Python.
If you (or your users) really can't upgrade you should use 'apply'.

--
Duncan Booth http://www.velocityreviews.com/forums/(E-Mail Removed)
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?
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      07-01-2003
In article <(E-Mail Removed)>,
Damir Hakimov <(E-Mail Removed)> wrote:
>
> [...]


....it never will be missed.
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

Apologies to G&S and everyone reading this.
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      07-01-2003
On Tue, 1 Jul 2003 11:39:15 +0400, "Damir Hakimov" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I have to have the WEP # set for file sharing? =?Utf-8?B?SGVscCBtZSBJIGNhbicndCBzaGFyZSE=?= Wireless Networking 3 11-01-2005 05:37 AM
have you got any of these i can have spike240 Case Modding 4 09-14-2005 03:48 AM
do i have to have.... =?Utf-8?B?amFrZQ==?= Wireless Networking 1 03-11-2005 06:05 PM
do I have to have a windows OS to start a New pc when the mwssage no ntldr foun darinsray Microsoft Certification 3 04-28-2004 06:50 AM
do I have to have a windows OS to start a New pc when the mwssage no ntldr foun darinsray Microsoft Certification 0 04-24-2004 06:15 AM



Advertisments