Daniel Choi
Daniel Choi

Reputation: 19

How to read numbers in a line into variables AND tuples in python

I have a file that contains a numbers in a single line like so:

6 10 11 2 23 37

The fourth number , represents how many numbers remain in the line. (the fourth number is 2 and the remaining two numbers are 23 and 27)

I know that I can assign individual integers to variable this way

a, b, c, d = map(int, line.split())

How do I read this file in python so that I can read the first four numbers into variables and the remaining numbers into tuples in python.

The Resulting output I want is

a = 6
b = 10
c = 11
d = 2
my_tuple = (23, 27)

What is the easiest way to go about this in python?

Upvotes: 1

Views: 135

Answers (1)

U13-Forward
U13-Forward

Reputation: 71580

Very close, just use my_tuple with unpacking (*) in the beginning:

a, b, c, d, *my_tuple = map(int, line.split())

And now:

print(a,b,c,d,my_tuple,sep='\n')

Outputs:

6
10
11
2
[23, 37]

Upvotes: 1

Related Questions