Python Forum
user input values into list of lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
user input values into list of lists
#1
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)
Reply
#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]]
Reply
#3
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
Reply
#4
for i in itertools.product(*conbinations)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 349 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  difference between forms of input a list to function akbarza 6 1,043 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  Copying the order of another list with identical values gohanhango 7 1,171 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,265 Nov-03-2023, 05:35 PM
Last Post: huzzug
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,074 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Comparing List values to get indexes Edward_ 7 1,183 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  restrict user input to numerical values MCL169 2 927 Apr-08-2023, 05:40 PM
Last Post: MCL169
  List all possibilities of a nested-list by flattened lists sparkt 1 925 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Adding values with reduce() function from the list of tuples kinimod 10 2,677 Jan-24-2023, 08:22 AM
Last Post: perfringo
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,106 Dec-25-2022, 03:00 PM
Last Post: askfriends

Forum Jump:

User Panel Messages

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