Python - Filter Strings within ASCII range
Last Updated :
08 Feb, 2024
Given ASCII or alphabetical range, filter strings are found in a particular range.
Input : test_list = ["gfg", "is", "best", "for", "geeks"], strt_asc, end_asc = 105, 115
Output : ['is']
Explanation : i has 105, and s has 115, which is in range ASCII values.
Input : test_list = ["gfg", "is", "best", "for", "geeks"], strt_asc, end_asc = 100, 115
Output : ['gfg', 'is', 'for', 'geeks']
Explanation : Strings with range characters included.
Method #1 : Using list comprehension + all() + ord()
In this, we check for all characters to be in the given ASCII range, computed using ord(), and accordingly, strings are filtered.
Python3
# Python3 code to demonstrate working of
# Filter Strings within ASCII range
# Using list comprehension + ord() + all()
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks"]
# printing original list
print("The original list is : " + str(test_list))
# initializing ASCII range
strt_asc, end_asc = 105, 115
# checking for all characters to be in ASCII range
res = [sub for sub in test_list if all(
ord(ele) >= strt_asc and ord(ele) <= end_asc for ele in sub)]
# printing result
print("Filtered Strings : " + str(res))
OutputThe original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Filtered Strings : ['is']
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), no extra space is required
Method #2 : Using filter() + lambda + all() + ord()
In this, we perform task of filtering using filter() and lambda function, ord() and all() is used in similar way as above method.
Python3
# Python3 code to demonstrate working of
# Filter Strings within ASCII range
# Using filter() + lambda + all() + ord()
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks"]
# printing original list
print("The original list is : " + str(test_list))
# initializing ASCII range
strt_asc, end_asc = 105, 115
# checking for all characters to be in ASCII range
res = list(filter(lambda sub: all(ord(ele) >= strt_asc and ord(
ele) <= end_asc for ele in sub), test_list))
# printing result
print("Filtered Strings : " + str(res))
OutputThe original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Filtered Strings : ['is']
Time Complexity: O(n*n)
Auxiliary Space: O(1)
METHOD 3:Using for
APPROACH:
This approach uses a for loop to iterate through each string in the given list and checks if each character in the string falls within the given ASCII range. If all characters in the string fall within the range, then the string is added to a new list.
ALGORITHM:
1.Initialize test_list, strt_asc, and end_asc with the given values.
2.Initialize an empty list result.
3.Iterate through each string in test_list.
4.For each string, iterate through each character in the string.
5.Check if the ASCII value of the character falls within the given range.
6.If all characters in the string fall within the range, add the string to the result list.
7.Print the result list.
Python3
test_list = ["gfg", "is", "best", "for", "geeks"]
strt_asc, end_asc = 105, 115
result = []
for s in test_list:
flag = True
for c in s:
if ord(c) < strt_asc or ord(c) > end_asc:
flag = False
break
if flag:
result.append(s)
print(result)
Time complexity: O(n * k) where n is the length of the given list and k is the length of the longest string in the list. This is because we need to iterate through each character in each string in the list.
Space complexity: O(n) since we are storing the filtered list in memory. The space required for strt_asc and end_asc is negligible.
Similar Reads
Python - Extract Strings with a digit Given a Strings List, extract those with atleast one digit. Input : test_list = ['gf4g', 'is', 'best', 'gee1ks'] Output : ['gf4g', 'gee1ks'] Explanation : 4, 1 are respective digits in string. Input : test_list = ['gf4g', 'is', 'best', 'geeks'] Output : ['gf4g'] Explanation : 4 is digit in string. M
5 min read
Python - Extract range sized strings Sometimes, while working with huge amount of data, we can have a problem in which we need to extract just specific range sized strings. This kind of problem can occur during validation cases across many domains. Letâs discuss certain ways to handle this in Python strings list. Method #1 : Using list
4 min read
Python | Filter String with substring at specific position Sometimes, while working with Python string lists, we can have a problem in which we need to extract only those lists that have a specific substring at a specific position. This kind of problem can come in data processing and web development domains. Let us discuss certain ways in which this task ca
5 min read
Python - Filter Tuples with Strings of specific characters Given a Tuple List, extract tuples, which have strings made up of certain characters. Input : test_list = [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')], char_str = 'gfestb' Output : [('gfg', 'best'), ('fest', 'gfg')] Explanation : All tuples contain characters from char_str. Input : test_list
5 min read
Check for ASCII String - Python To check if a string contains only ASCII characters, we ensure all characters fall within the ASCII range (0 to 127). This involves comparing each character's value to ensure it meets the criteria.Using str.isascii()The simplest way to do this in Python is by using the built-in str.isascii() method,
2 min read
Check for ASCII String - Python To check if a string contains only ASCII characters, we ensure all characters fall within the ASCII range (0 to 127). This involves comparing each character's value to ensure it meets the criteria.Using str.isascii()The simplest way to do this in Python is by using the built-in str.isascii() method,
2 min read