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
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
strt_asc, end_asc = 105 , 115
res = [sub for sub in test_list if all (
ord (ele) > = strt_asc and ord (ele) < = end_asc for ele in sub)]
print ( "Filtered Strings : " + str (res))
|
Output
The 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
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
strt_asc, end_asc = 105 , 115
res = list ( filter ( lambda sub: all ( ord (ele) > = strt_asc and ord (
ele) < = end_asc for ele in sub), test_list))
print ( "Filtered Strings : " + str (res))
|
Output
The 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.
Share your thoughts in the comments
Please Login to comment...