Hướng dẫn product of tuple python
Let's say I have a
What's everyone's take on line 12?
I've heard that explicit list index references should be avoided. Is there a function I can use that acts like Thanks for the help everyone. asked Oct 22, 2011 at 19:11
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 :
answered Dec 2, 2020 at 15:05
n49o7n49o7 4266 silver badges8 bronze badges 2 I don't see any problem with using indexes here:
If you really want to avoid them, you can do:
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.
Built-in? No. But you can get that functionality rather simply with the following:
answered Oct 22, 2011 at 19:24
AvarisAvaris 34.6k7 gold badges77 silver badges71 bronze badges 3 In short, just use
Which is in your use case
answered Mar 7, 2017 at 13:08
cwilmotcwilmot 991 silver badge5 bronze badges 0 you can do:
but I think it just makes things less readable. However, before summing you are actually building
the list of multiplication
This is not needed, simply do:
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"
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. |