How to get integers from a tuple in Python?

I have a tuple with two numbers in it, I need to get both numbers. The first number is the x coordinate, and the second is the y coordinate. My pseudo code is my idea on how to do this, however I'm not quite sure how to make it work.

pseudo code:

tuple = (46, 153)
string = str(tuple)
ss = string.search()
int1 = first_int(ss) 
int2 = first_int(ss) 
print int1
print int2

int1 will return 46, and int2 will return 153.

+3
source share
3 answers
int1, int2 = tuple
+25
source

Another way is to use array indices:

int1 = tuple[0]
int2 = tuple[1]

This is useful if you find that you need at some point to access one member of a tuple.

+22
source

The third way is to use the new namedtuple type:

from collections import namedtuple
Coordinates = namedtuple('Coordinates','x,y')
coords = Coordinates(46,153)
print coords
print 'x coordinate is:',coords.x,'y coordinate is:',coords.y
+6
source

Source: https://habr.com/ru/post/1755468/


All Articles