Python Forum

Full Version: user input values into list of lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following code :

import itertools
from functools import reduce
CountOfCombination= 0
for i in itertools.product((1,2),(1,2,0),(1,2)):
       CountOfCombination += 1
       print('For combination {0} is product of values {1} and sum{2}'.format(i, reduce(lambda a, b: a*b,i),reduce(lambda a, b : a+b,i)))

print ('Count of combination is {0}'.format(CountOfCombination))
I would like to populate a variable for the cartesian product via user input, something like this :

import itertools
cont = ""
index = 0
a=[]
while (cont != "N"):
    tips = input("Input combination :  ")
    for i in range(len(tips)):
        a[index][i] = tips[i]
        index += 1
        cont = input("Contiune? [Y/N]")
where a[] would replace
(1,2),(1,2,0),(1,2)
conbinations = []
while True:
    if combination := input("Input the combination: "):
        combination = list(map(float, combination.split()))
        conbinations.append(combination)
    else:
        break
print(conbinations)
Output:
Input the combination: 1 2 Input the combination: 1 2 0 Input the combination: 1 2 Input the combination: [[1.0, 2.0], [1.0, 2.0, 0.0], [1.0, 2.0]]
it's not working the way I need it to.
import itertools
from functools import reduce
conbinations = []
while True:
    if combination := input("Input the combination: "):
        combination = list(map(float, combination.split()))
        conbinations.append(combination)
    else:
        break
CountOfCombination= 0
for i in itertools.product(conbinations):
       CountOfCombination += 1
       print('For combination {0} is product of values {1} and sum{2}'.format(i, reduce(lambda a, b: a*b,i),reduce(lambda a, b : a+b,i)))

print ('Count of combination is {0}'.format(CountOfCombination))
Output:
/usr/bin/python3.10 /home/tauros/PycharmProjects/jaggedArray/venv/test1.py Input the combination: 1 2 Input the combination: 1 0 2 Input the combination: For combination ([1.0, 2.0],) is product of values [1.0, 2.0] and sum[1.0, 2.0] For combination ([1.0, 0.0, 2.0],) is product of values [1.0, 0.0, 2.0] and sum[1.0, 0.0, 2.0] Count of combination is 2 Process finished with exit code 0
I need this result :
Output:
For combination (1, 1) is product of values 1 and sum 2 For combination (1, 0) is product of values 0 and sum 1 For combination (1, 2) is product of values 2 and sum 3 For combination (2, 1) is product of values 2 and sum 3 For combination (2, 0) is product of values 0 and sum 2 For combination (2, 2) is product of values 4 and sum 4 Count of combination is 6 Process finished with exit code 0
for i in itertools.product(*conbinations)