Python – Extract digits from Tuple list

Extract digits from Tuple list: Sometimes while working with lists. we can have a problem in which we need to perform extraction of all the di-gits from this lists.This kind of problem can find its application in data domains and day-day programming. Python can perform a wide range of data manipulation tasks.

We have a case where we are provided a list with entries that are tuples of two integers. In this tutorial, we’ll look at how to extract the digits from tuple items in a list by two different methods.

Need to Python – Extract digits from Tuple list List

When dealing with Python lists, we may encounter an issue where we need to remove all of the numbers from a tuple list. Let us have a look at the examples to understand the input-output format:

How do I extract two numbers from a cell?
A way to do this is:
Select all your data – assuming it is in a string all the time – which means one cell has

one row with ID&RXN nos. …
Go to the Data tab, Text to columns.
Choose Delimited >> Next >> choose Space here, in Other , type a colon( : ) >> Finish.

How do I get Python – Extract digits from Tuple list List Excel to count text?


Image result for Need to Extract Digit
Select a blank cell where you want to return the first number from a text string, enter the formula



1:"&LEN(A2)))),ISNUMBER(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)+0)ROW(INDIRECT("1:"&LEN(A2))))),1)+0 (A2 is the text cell where you will look …

Input: List = [(25, 13), (22, 19)]

Output: [9, 5, 3, 1, 2]

Input: List = [(25, 13), (13, 19), (11, 10), (99, 12)]

Output: [0, 5, 3, 1, 2, 9]

What does extract the digit mean?

Making use of isdigit function to extract digits from a Python string. Python provides us with string. isdigit to check for the presence of digits in a string. Python isdigit function returns True if the input string contains digit characters in it.DUse it to pull pieces of information out of longer strings, such as addresses or project numbers.

How do you extract digits in eed to Python – Extract digits from Tuple list List ?

Making use of isdigit() function to extract digits from a Python string. Python provides us with string. isdigit() to check for the presence of digits in a string. Python isdigit() function returns True if the input string contains digit characters in it.

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

This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML.The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.

Method #1 diagram

For instance, SML provides a tabulation tool: tabulate(f) which produces a sequence f(0), f(1), …. These tools and their built-in counterparts also work well with the high-speed functions in the operator module.

# Python3 code to demonstrate working of
# Extract digits from Tuple list
# Using map() + chain.from_iterable() + set() + loop
from itertools import chain

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

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

# Extract digits from Tuple list
# Using map() + chain.from_iterable() + set() + loop
temp = map(lambda ele: str(ele), chain.from_iterable(test_list))
res = set()
for sub in temp:
	for ele in sub:
		res.add(ele)

# printing result
print("The extracted digits : " + str(res))

The following module functions all construct and return iterators.

itertools.accumulate(iterable[, func, *, initial=None])
Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional func argument).(For example, with the default operation of addition, elements may be any addable type including Decimal or Fraction.).

Usually, the number of elements output matches the input iterable. However, if the keyword argument initial is provided, the accumulation leads off with the initial value so that the output has one more element than the input iterable.

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 

Another method to solve the problem is to use regular expressions to extract. All the digits from the list of tuples and then store them into a set to make sure. You can use regular expressions in OQL and in stitcher language code.

Regular expressions are particularly useful for defining filters.Regular expressions contain a series of characters that define a pattern of text to be matched—to make a filter more specialized, or general.

For example, the regular expression ^AL[.]* searches for all items beginning with AL. The filter condition EntityName Like ^..N[.]* filters for all devices which have an N as the third letter of their name, and EntityName Like [.]*G filters for all devices whose name ends with the letter G. The following table describes the most common characters used in regular expressions.

Method #2 diagram

# Python3 code to demonstrate working of
# Extract digits from Tuple list
# Using regex expression
import re

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

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

# Extract digits from Tuple list
# Using regex expression
temp = re.sub(r'[\[\]\(\), ]', '', str(test_list))
res = [int(ele) for ele in set(temp)]

# printing result
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]

Leave a Reply