Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get integers from a tuple in Python?

Tags:

python

tuples

I have a tuple with two numbers in it, I need to get both numbers. The first number is the x-coordinate, while the second is the y-coordinate. My pseudo code is my idea about how to go about it, 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 would return 46, while int2 would return 153.

like image 370
rectangletangle Avatar asked Jul 20 '10 08:07

rectangletangle


People also ask

How do you access numbers in a tuple?

We can use the index operator [] to access an item in a tuple, where the index starts from 0. So, a tuple having 6 elements will have indices from 0 to 5.

How do you find the number of elements in a tuple in Python?

Python tuple method len() returns the number of elements in the tuple.

How do you convert a single tuple to an integer in Python?

Method #2 : Using int() + join() + map() The combination of these functions can also be used to perform this task. In this, we convert each element to string using join() and iterate using map(). At last we perform integer conversion.


2 Answers

int1, int2 = tuple
like image 95
relet Avatar answered Sep 20 '22 16:09

relet


The other way is to use array subscripts:

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

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

like image 20
Skilldrick Avatar answered Sep 21 '22 16:09

Skilldrick