Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Product expression.
#1
What I want to do it to use Product from itertools.

I want the length of the product to be variable.

so for example...
var = 'AB'

If I had product(var, var)
I would get
AA
AB
BA
BB

If product(var, var, var)
AAA
AAB
ABA etc...

what if want to make it so I can just tell product I want var 2x or 3 x or 4x such as:
product(var, var)
product(var, var, var)
product(var, var, var, var)

how can I write a simple function so that I send a number for how many times I want to feed var into the product function then get my object back?

Thanks

UPDATE:

Never mind I figured out I can feed a list of lists into Product with a *.
example:
var = [['A', 'B'], ['A', 'B']]
myObject = product(*var)
Reply
#2
Did you check the docs? Did you notice the optional repeat parameter?
From the docs
Quote:To compute the product of an iterable with itself, specify the number of repetitions with the optional repeat keyword argument. For example, product(A, repeat=4) means the same as product(A, A, A, A).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information Showing trendline formula in a table per product Carlossxx 0 629 May-03-2023, 08:34 AM
Last Post: Carlossxx
  How to add product details in exe generated by pyinstaller arex786 1 8,252 Oct-10-2021, 11:00 AM
Last Post: Sran012
  Largest product in a grid (projecteuler problem11) tragical 1 2,241 Sep-14-2020, 01:03 PM
Last Post: Gribouillis
  Pass results of expression to another expression cmdr_eggplant 2 2,238 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  Blending calculator from final product xerxes106 0 1,588 Dec-05-2019, 10:32 AM
Last Post: xerxes106
  Make dual vector dot-product more efficient technossomy 3 2,487 Nov-28-2019, 09:27 PM
Last Post: Gribouillis
  Store a product/item in a inventory program viktoria_linn 1 4,023 Jul-02-2019, 09:26 PM
Last Post: DeaD_EyE
  Product of maximum in first array and minimum in second Thethispointer 9 5,204 Jan-19-2018, 07:38 PM
Last Post: Thethispointer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020