Home > Back-end >  How to take a tuple of two lists from user input?
How to take a tuple of two lists from user input?

Time:05-14

How to take a tuple of two equal-size lists from user input at a bash terminal from within a python script? Let's assume we want the program to register the following tuple

([0.001, 0.01, 0.1, 1], [1000, 100, 10, 1])

The only condition to grab them is to do that in pairs of two: 0.001, 1000 and then 0.01, 100 and etc.


Explanation

the code takes 0.001 and 1000 first

it then takes 0.01 and 100 second

it then takes 0.1 and 10 third

and finally it takes 1 and 1.

Once it has taken all of them, it will arrange them in the said tuple.

CodePudding user response:

You can use loop to take inputs.

lst1 = list()
lst2 = list()
for i in range(4):
    lst1.append(input("input 1 :"))
    lst2.append(input("input 2 :"))

tup = (lst1, lst2)

CodePudding user response:

N = 4
# user input in this way:
# 1 2
x =  [tuple(map(lambda x: int(x), input().split())) for _ in range(N)]

# user input in this way:
# 1 
# 2
# x = [(int(input()), int(input())) for _ in range(N)]

if you just want to extract the input:

x, y = ([0.001, 0.01, 0.1, 1], [1000, 100, 10, 1])
print(tuple(zip(x,y)))
# ((0.001, 1000), (0.01, 100), (0.1, 10), (1, 1))
  • Related