user input values into list of lists - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: user input values into list of lists (/thread-39071.html) |
user input values into list of lists - tauros73 - Dec-29-2022 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) RE: user input values into list of lists - deanhystad - Dec-29-2022 conbinations = [] while True: if combination := input("Input the combination: "): combination = list(map(float, combination.split())) conbinations.append(combination) else: break print(conbinations)
RE: user input values into list of lists - tauros73 - Dec-29-2022 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)) I need this result :
RE: user input values into list of lists - deanhystad - Dec-29-2022 for i in itertools.product(*conbinations) |