A Short Question on list

J

joy99

Dear Group,
If I have a list of elements, like,
list=[1,2,3,4,5,..]
now, if I want to multiply an increment of subset of the list each
time,
like,

elem1_list=list[0]
elem2_list=list[1]
elem3_list=list[2]
elem4_list=list[3]

multiplysubset1=elem1_list*elem2_list
multiplysubset2=elem1_list*elem2_list
multiplysubset3=elem1_list*elem2_list*elem3_list
…….
………

As I was figuring out doing a for kind of iteration may not help
exactly
Can any one give some idea?

Best Regards,
Subhabrata.
 
C

Corey Richardson

Dear Group,
If I have a list of elements, like,
list=[1,2,3,4,5,..]
now, if I want to multiply an increment of subset of the list each
time,
like,

elem1_list=list[0]
elem2_list=list[1]
elem3_list=list[2]
elem4_list=list[3]

Why do you assign those to variables?
As I was figuring out doing a for kind of iteration may not help
exactly
Can any one give some idea?

Are you looking for something like this?# UNTESTED (Poor styling of variable names is noted)

def recursiveMultiply(l, r, product): # list, range (no shadowing)
if r <= 0:
return sum
product *= l[r]
recursiveMultiply(l, r - 1, product) # r must be decremented

subsetrange = 15
multiplysubset, elemlist = [], list(range(15)) # Works with Py2 and Py3

assert subset_range <= len(elemlist)

for i in range(subsetrange):
multiplysubset.append(recursiveMultiply(elemlist, i, 0))
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top