A Short Question on list
Corey Richardson
kb1pkl at aim.com
Thu Mar 3 08:38:50 EST 2011
On 03/03/2011 08:08 AM, joy99 wrote:
> 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))
--
Corey Richardson
More information about the Python-list
mailing list