How to extract numbers from 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, 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.

SilentGhost

294k64 gold badges301 silver badges291 bronze badges

asked Jul 20, 2010 at 8:37

rectangletanglerectangletangle

48k91 gold badges198 silver badges272 bronze badges

3

answered Jul 20, 2010 at 8:39

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.

answered Jul 20, 2010 at 8:40

SkilldrickSkilldrick

67.7k33 gold badges172 silver badges227 bronze badges

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

answered Jul 20, 2010 at 10:48

a way better way is using *:

a = (1,2,3)
b = [*a]
print(b)

it gives you a list

answered Mar 18, 2020 at 22:28

JasonJason

2,8102 gold badges19 silver badges33 bronze badges

Returns a match where the string contains digits (numbers from 0-9)

import re
tl = [(1, 11), (5, 9) , (6,3)]

list1 = re.findall(r'\d+',str(tl))

tlstr = ''.join(list1)

num = list(set(tlstr))
print(num)

How to extract numbers from tuple in python

Ty Hitzeman

8351 gold badge13 silver badges24 bronze badges

answered Feb 25, 2021 at 16:18

Sometimes, while working with Python lists, we can have a problem in which we need to perform extraction of all the digits from tuple list. This kind of problem can find its application in data domains and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(15, 3), (3, 9)] 
Output : [9, 5, 3, 1]

Input : test_list = [(15, 3)] 
Output : [5, 3, 1] 
 

Method #1: Using map() + chain.from_iterable() + set() + loop 
The combination of above functions can be used to solve this problem. In this, we perform the task of flattening list using chain.from_iterable(), and then the digits are extracted using brute method. set() is used to remove duplicate digits.

Python3

from itertools import chain

test_list = [(15, 3), (3, 9), (1, 10), (99, 2)]

print("The original list is : " + str(test_list))

temp = map(lambda ele: str(ele), chain.from_iterable(test_list))

res = set()

for sub in temp:

    for ele in sub:

        res.add(ele)

print("The extracted digits : " + str(res))

Output

The original list is : [(15, 3), (3, 9), (1, 10), (99, 2)]
The extracted digits : {'1', '0', '3', '2', '9', '5'}

Method #2: Using regex expression 
This is yet another way in which this task can be performed. In this, an appropriate regex expression is used to extract the required unique digits.

Python3

import re

test_list = [(15, 3), (3, 9), (1, 10), (99, 2)]

print("The original list is : " + str(test_list))

temp = re.sub(r'[\[\]\(\), ]', '', str(test_list))

res = [int(ele) for ele in set(temp)]

print("The extracted digits : " + str(res))

Output

The original list is : [(15, 3), (3, 9), (1, 10), (99, 2)]
The extracted digits : [5, 9, 2, 0, 1, 3]

Method #3: Using list(),str(),map(),set() methods .

Initially converted all elements of tuple to string and concatenated them.Later used set() method to remove the duplicates, converted string elements to integer elements and finally converted them to list datatype.

Python3

test_list = [(15, 3), (3, 9), (1, 10), (99, 2)]

print("The original list is : " + str(test_list))

x=""

for i in test_list:

    for j in i:

        x+=str(j)

res=list(map(int,set(x)))

print("The extracted digits : " + str(res))

Output

The original list is : [(15, 3), (3, 9), (1, 10), (99, 2)]
The extracted digits : [2, 3, 0, 1, 9, 5]


How do you extract digits from a tuple?

In this, we perform the task of flattening list using chain. from_iterable(), and then the digits are extracted using brute method. set() is used to remove duplicate digits.

How do I extract data from a tuple in Python?

Python - Unpack Tuples.
❮ Previous Next ❯.
Packing a tuple: fruits = ("apple", "banana", "cherry") ... .
Unpacking a tuple: fruits = ("apple", "banana", "cherry") ... .
Assign the rest of the values as a list called "red": ... .
Add a list of values the "tropic" variable: ... .
❮ Previous Next ❯.

How do you get an integer from a tuple in Python?

You only have to use the int() class if your tuple doesn't store integers. Otherwise, directly access the tuple element at its index. Copied! You can also convert a tuple to an integer by using the sum() function or multiplying its values.

How do I extract numeric values in Python?

Summary: To extract numbers from a given string in Python you can use one of the following methods:.
Use the regex module..
Use split() and append() functions on a list..
Use a List Comprehension with isdigit() and split() functions..
Use the num_from_string module..

How do I find a number in tuple?

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