Hướng dẫn product of tuple python

Let's say I have a

class Rectangle(object):                                               
def __init__(self, length, width, height=0):                                                   
    self.l = length                                               
    self.w = width                                                
    self.h = height                                               
    if not self.h:                                                     
        self.a = self.l * self.w                                       
    else:                                                              
        from itertools import combinations                            
        args = [self.l, self.w, self.h]                                
        self.a = sum(x*y for x,y in combinations(args, 2)) * 2
                 # original code:
                 # (self.l * self.w * 2) + \                            
                 # (self.l * self.h * 2) + \                            
                 # (self.w * self.h * 2)                                
        self.v = self.l * self.w * self.h                                           

What's everyone's take on line 12?

self.a = sum(x*y for x,y in combinations(args, 2)) * 2 

I've heard that explicit list index references should be avoided.

Is there a function I can use that acts like sum(), but only for multiplication?

Thanks for the help everyone.

asked Oct 22, 2011 at 19:11

Hướng dẫn product of tuple python

yurisichyurisich

6,8155 gold badges41 silver badges62 bronze badges

2

Since this is in the top Google results, I'll just add that since Python 3.8, you can do :

from math import prod
t = (5, 10)
l = [2, 100]
prod(t) # 50
prod(l) # 200

answered Dec 2, 2020 at 15:05

n49o7n49o7

4266 silver badges8 bronze badges

2

I don't see any problem with using indexes here:

sum([x[0] * x[1] for x in combinations(args, 2)])

If you really want to avoid them, you can do:

sum([x*y for x,y in combinations(args, 2)])

But, to be honest I would prefer your commented out version. It is clear, readable and more explicit. And you don't really gain much by writing it as above just for three variables.

Is there a function I can use that acts like sum(), but only for multiplication?

Built-in? No. But you can get that functionality rather simply with the following:

In : a=[1,2,3,4,5,6]

In : from operator import mul

In : reduce(mul,a)
Out: 720

answered Oct 22, 2011 at 19:24

AvarisAvaris

34.6k7 gold badges77 silver badges71 bronze badges

3

In short, just use np.prod

import numpy as np
my_tuple = (2, 3, 10)
print(np.prod(my_tuple))  # 60

Which is in your use case

np.sum(np.prod(x) for x in combinations(args, 2))

np.prod can take both lists and tuple as a parameter. It returns the product you want.

answered Mar 7, 2017 at 13:08

cwilmotcwilmot

991 silver badge5 bronze badges

0

you can do:

from operator import mul
sum(reduce(mul,combinations(args, 2)))

but I think it just makes things less readable.

However, before summing you are actually building the list of multiplication sum([...]).

self.a = sum([(x[0] * x[1] * 2) for x in combinations(args, 2)])

This is not needed, simply do:

self.a = sum(x * y * 2 for x,y in combinations(args, 2))

answered Oct 22, 2011 at 19:35

log0log0

10.2k3 gold badges25 silver badges61 bronze badges

4

I did make a very simple definition of product; helpful for "calculating the product of a tuple"

def product(tuple1):
    """Calculates the product of a tuple"""
    prod = 1
    for x in tuple1:
        prod = prod * x
    return prod

Might be a more elegant way to do it but this seems to work OK. Presumably it would work on a list just as well.

iMom0

12k3 gold badges48 silver badges61 bronze badges

answered Feb 11, 2012 at 4:32

Not the answer you're looking for? Browse other questions tagged python standard-library or ask your own question.