Hello gurus,
I have a question, a function like below, it is implemented by me, :)
def funcA(tarray):
a = [2,3,4]
if len(tarray) >=3:
return a[0],a[1], a[2]
elif len(tarray) == 2:
return a[0],a[1], funcB(1)[0]
elif len(tarray) == 1:
return a[0], funcB(2)[0], funcB(2)[1]
else:
return funcB(3)[0], funcB(3)[1], funcB(3)[2]
The return of funcA is always 3 values, but depending on the length of
tarray, I need to return different values accordingly. if tarray lenght is
2, I need to get another one value from funcB, if tarray length is 0, I need
to get all three values from funcB.
Is there a brief way to achieve it?
Thanks, 5 1212
On Tue, 23 Oct 2007 16:28:37 +0800, Yinghe Chen wrote:
Hello gurus,
I have a question, a function like below, it is implemented by me, :)
def funcA(tarray):
a = [2,3,4]
if len(tarray) >=3:
return a[0],a[1], a[2]
elif len(tarray) == 2:
return a[0],a[1], funcB(1)[0]
elif len(tarray) == 1:
return a[0], funcB(2)[0], funcB(2)[1]
else:
return funcB(3)[0], funcB(3)[1], funcB(3)[2]
The return of funcA is always 3 values, but depending on the length of
tarray, I need to return different values accordingly. if tarray lenght is
2, I need to get another one value from funcB, if tarray length is 0, I need
to get all three values from funcB.
Untested:
def func_a(t_array):
result = [2, 3, 4]
t_array_length = len(t_array)
remaining_length = len(result) - t_array_length
if t_array_length < len(result):
result = (result[:t_array_length]
+ func_b(remaining_length)[:remaining_length])
return tuple(result)
Ciao,
Marc 'BlackJack' Rintsch
"Yinghe Chen" <yi*********@jeppesen.com.rmwrites:
def funcA(tarray):
a = [2,3,4]
if len(tarray) >=3:
return a[0],a[1], a[2]
elif len(tarray) == 2:
return a[0],a[1], funcB(1)[0]
elif len(tarray) == 1:
return a[0], funcB(2)[0], funcB(2)[1]
else:
return funcB(3)[0], funcB(3)[1], funcB(3)[2]
untested:
from itertools import chain, islice
def funcA(tarray):
xB = max(3 - len(tarray), 0)
return chain(a, islice(funcB(xB), xB))
even shorter:
def funcA(tarray):
s = min(len(tarray), 3)
return [2, 3, 4][0:s] + [e for e in funcB(3-s)[0:3-s]]
On Tue, 23 Oct 2007 11:48:08 +0200, Loic Mahe wrote:
even shorter:
def funcA(tarray):
s = min(len(tarray), 3)
return [2, 3, 4][0:s] + [e for e in funcB(3-s)[0:3-s]]
Why the list comprehension!?
Ciao,
Marc 'Blackjack' Rintsch
Marc 'BlackJack' Rintsch a écrit :
On Tue, 23 Oct 2007 11:48:08 +0200, Loic Mahe wrote:
>even shorter:
def funcA(tarray): s = min(len(tarray), 3) return [2, 3, 4][0:s] + [e for e in funcB(3-s)[0:3-s]]
Why the list comprehension!?
Ciao,
Marc 'Blackjack' Rintsch
sorry I just read too fast
and thought he worked with lists ...
anyway 'e for e in' and so list comprehension was useless here
def funcA(tarray):
s = min(len(tarray), 3)
return (2, 3, 4,)[0:s] + funcB(3-s)[0:3-s]
this is ok if funcB(...) returns a tuple ...
if it returns a list just add: tuple(funcB(...))
note: list comprehension transforms a tuple into a list This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Oplec |
last post by:
Hi,
I am learning standard C++ as a hobby. The C++ Programming Language :
Special Edition has been the principal source for my information. I read the
entirety of the book and concluded that I...
|
by: Charles Law |
last post by:
This is going to seem like a basic OO question, but it comes up and bites me
every now and again.
Suppose we have a multi-tiered protocol to implement, what is the logical,
OO way to design the...
|
by: wukexin |
last post by:
I write my own class Cfile, I want to know what about implement ctime().Who
help me?
My use function ctime, I sign it with $$$.
my class Cfile:
#------------------------
file.h...
|
by: Don |
last post by:
Can anyone give me an example of implementing ICloneable to give a class I
created a "Clone" method so I can make copies of objects. I have no idea
where to begin with this. Thanks.
- Don
|
by: GrandpaB |
last post by:
Hi,
I recently had a post about how to block Mousewheel events. The answer was
to implement an IMessageFilter. Sadly, I must report that after 24 hours of
researching my library and online...
|
by: Stacey Levine |
last post by:
I have a webservice that I wanted to return an ArrayList..Well the service
compiles and runs when I have the output defined as ArrayList, but the WSDL
defines the output as an Object so I was...
|
by: alex |
last post by:
I want to implement an async-function,just like Oracle OCI function:
for example:
I call oci "execute(strSQL)" function, and I can call "cancle"
function to cancle the process if the SQL...
|
by: emin.shopper |
last post by:
I had a need recently to check if my subclasses properly implemented
the desired interface and wished that I could use something like an
abstract base class in python. After reading up on metaclass...
|
by: UG |
last post by:
I just wanted to know whether any timer facility exists in C, as it is
not mentioned in K&R 2, or in the ISO Draft. By timer function i mean
that when we use standard input function like scanf() or...
|
by: shnaqawi |
last post by:
Dear Friends,
I have an objectdatasource which is bound to a details view. the no. of columns are variable. I could do the select function by defining a dynamic sqlSelectCommand. The problem now...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |