Python program to extract only the numbers from a list which have some specific digits
Last Updated :
06 Apr, 2023
Given the elements List, extract numbers with specific digits.
Input : test_list = [3456, 23, 128, 235, 982], dig_list = [2, 3, 5, 4]
Output : [23, 235]
Explanation : 2, 3 and 2, 3, 5 are in digit list, hence extracted elements.
Input : test_list = [3456, 23, 28, 235, 982], dig_list = [2, 3, 5, 4, 8]
Output : [23, 28, 235]
Explanation : 2, 3; 2, 8 and 2, 3, 5 are in digit list, hence extracted elements.
Method #1 : Using list comprehension + all()
In this, we check for each element in number against the elements from target list to be present, if all are found in list, element is returned.
Python3
test_list = [ 345 , 23 , 128 , 235 , 982 ]
print ( "The original list is : " + str (test_list))
dig_list = [ 2 , 3 , 5 , 4 ]
res = [sub for sub in test_list if all (
int (ele) in dig_list for ele in str (sub))]
print ( "Extracted elements : " + str (res))
|
Output
The original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using filter() + lambda + all()
In this, filtering of elements is done using filter() + lambda, all() is used to check for all the digits from other list.
Python3
test_list = [ 345 , 23 , 128 , 235 , 982 ]
print ( "The original list is : " + str (test_list))
dig_list = [ 2 , 3 , 5 , 4 ]
res = list ( filter ( lambda sub: all (
int (ele) in dig_list for ele in str (sub)), test_list))
print ( "Extracted elements : " + str (res))
|
Output
The original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the built-in filter() + lambda + all() function which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of the input list.
Method #3 : Using Counter() + keys()+ all()
Python3
from collections import Counter
test_list = [ 345 , 23 , 128 , 235 , 982 ]
print ( "The original list is : " + str (test_list))
dig_list = [ 2 , 3 , 5 , 4 ]
freq = Counter(dig_list)
res = [sub for sub in test_list if all (
int (ele) in freq.keys() for ele in str (sub))]
print ( "Extracted elements : " + str (res))
|
Output
The original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time Complexity: O(n) as we used hashing for searching whether the digit is present in dig_list or not
Method #4: Using itertools.filterfalse() method
Python3
import itertools
test_list = [ 345 , 23 , 128 , 235 , 982 ]
print ( "The original list is : " + str (test_list))
dig_list = [ 2 , 3 , 5 , 4 ]
res = list (itertools.filterfalse( lambda sub : not all ( int (ele) in dig_list for ele in str (sub)), test_list))
print ( "Extracted elements : " + str (res))
|
Output
The original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time Complexity:O(N)
Auxiliary Space:O(N)
Method #5:Using recursion
Algorithm:
- Define a function get_elements_with_specific_digits() that takes a list test_list and a digit list dig_list as input.
- Check if the input list test_list is empty. If it is, return an empty list.
- If the input list test_list is not empty, take the first element sub of the list.
- Check if all of the digits in sub are in the digit list dig_list.
- If all of the digits in sub are in dig_list, add sub to the result list and call the function recursively on the remaining elements of test_list (i.e., the elements starting from the second element).
- If not all of the digits in sub are in dig_list, call the function recursively on the remaining elements of test_list (i.e., the elements starting from the second element).
- Return the result list.
Python3
def get_elements_with_specific_digits(test_list, dig_list):
if not test_list:
return []
sub = test_list[ 0 ]
if all ( int (ele) in dig_list for ele in str (sub)):
return [sub] + get_elements_with_specific_digits(test_list[ 1 :], dig_list)
else :
return get_elements_with_specific_digits(test_list[ 1 :], dig_list)
test_list = [ 345 , 23 , 128 , 235 , 982 ]
print ( "The original list is : " + str (test_list))
dig_list = [ 2 , 3 , 5 , 4 ]
res = get_elements_with_specific_digits(test_list, dig_list)
print ( "Extracted elements : " + str (res))
|
Output
The original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time complexity: O(n * m), where n is the length of the input list and m is the maximum length of an integer in the list (i.e., the number of digits in the largest integer). This is because we iterate through the entire list and for each integer in the list, we check if each of its digits is in the digit list.
Auxiliary space: O(n), where n is the length of the input list. This is because we create a new list to store the filtered elements.
Method 6: Use a for a loop
Approach:
- Convert the digit list into a set for O(1) lookup time.
- Initialize an empty list to store the filtered elements.
- Iterate through each element of the test_list using a for loop.
- Convert the current element to a string and convert each character to an integer using map() function.
- Check if the set of digits in the digit list is a subset of the set of digits in the current element.
- If it is, append the current element to the result list.
- Return the result list.
- Here is the code for the above approach with time and auxiliary space complexity:
Python3
def get_elements_with_specific_digits(test_list, dig_list):
dig_set = set (dig_list)
res = []
for ele in test_list:
digits = set ( map ( int , str (ele)))
if digits.issubset(dig_set):
res.append(ele)
return res
test_list = [ 345 , 23 , 128 , 235 , 982 ]
print ( "The original list is : " + str (test_list))
dig_list = [ 2 , 3 , 5 , 4 ]
res = get_elements_with_specific_digits(test_list, dig_list)
print ( "Extracted elements : " + str (res))
|
Output
The original list is : [345, 23, 128, 235, 982]
Extracted elements : [345, 23, 235]
Time complexity: O(n * k), where n is the length of the test_list and k is the maximum number of digits in any element of the test_list.
Auxiliary space: O(n), where n is the length of the test_list.
Similar Reads
Python Program to remove a specific digit from every element of the list
Given a list of elements, the task here is to write a Python program that can remove the presence of all a specific digit from every element and then return the resultant list. Examples: Input : test_list = [333, 893, 1948, 34, 2346], K = 3 Output : ['', 89, 1948, 4, 246] Explanation : All occurrenc
7 min read
Python Program to extracts elements from the list with digits in increasing order
Given a List of elements, extract all elements which have digits that are increasing in order. Input : test_list = [1234, 7373, 3643, 3527, 148, 49] Output : [1234, 148, 49] Explanation : All elements have increasing digits.Input : test_list = [12341, 7373, 3643, 3527, 1481, 491] Output : [] Explana
5 min read
Python Program to test if the String only Numbers and Alphabets
Given a String, our task is to write a Python program to check if string contains both numbers and alphabets, not either nor punctuations. Examples: Input : test_str = 'Geeks4Geeks' Output : True Explanation : Contains both number and alphabets. Input : test_str = 'GeeksforGeeks' Output : False Expl
4 min read
Python program to find tuples which have all elements divisible by K from a list of tuples
Given a list of tuples. The task is to extract all tuples which have all elements divisible by K. Input : test_list = [(6, 24, 12), (60, 12, 6), (12, 18, 21)], K = 6 Output : [(6, 24, 12), (60, 12, 6)] Explanation : Both tuples have all elements multiple of 6. Input : test_list = [(6, 24, 12), (60,
7 min read
Python program to find the smallest number in a file
Given a text file, write a Python program to find the smallest number in the given text file. Examples: Input: gfg.txtOutput: 9Explanation: Contents of gfg.txt: I live at 624 Hyderabad.My mobile number is 52367. My favourite number is 9.Numbers present in the text file are 9,624,52367Minimum number
3 min read
Python program to calculate the number of digits and letters in a string
In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list. [GFGTABS] Python s = "Hello123!
3 min read
Python Program to replace list elements within a range with a given number
Given a range, the task here is to write a python program that can update the list elements falling under a given index range with a specified number. Input : test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1], i, j = 4, 8, K = 9 Output : [4, 6, 8, 1, 9, 9, 9, 9, 12, 3, 9, 1] Explanation : List is u
3 min read
Python Program to Delete Specific Line from File
In this article, we are going to see how to delete the specific lines from a file using Python Throughout this program, as an example, we will use a text file named months.txt on which various deletion operations would be performed. Method 1: Deleting a line using a specific positionIn this method,
3 min read
Python - Sort list of numbers by sum of their digits
Sorting a list of numbers by the sum of their digits involves ordering the numbers based on the sum of each individual digit within the number. This approach helps prioritize numbers with smaller or larger digit sums, depending on the use case. Using sorted() with a Lambda Functionsorted() function
2 min read
Python Program for Remove leading zeros from a Number given as a string
Given numeric string str, the task is to remove all the leading zeros from a given string. If the string contains only zeros, then print a single "0". Examples: Input: str = "0001234" Output: 1234 Explanation: Removal of leading substring "000" modifies the string to "1234". Hence, the final answer
3 min read