Open In App

Python – Character coordinates in Matrix

Sometimes, while working with Python data, we can have a problem in which we need to extract all the coordinates in Matrix, which are characters. This kind of problem can have potential application in domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [‘1G’, ’12F’, ‘231G’] 
Output : [(0, 1), (1, 2), (2, 3)] 



Input : test_list = [‘G’, ‘F’, ‘G’] 
Output : [(0, 0), (1, 0), (2, 0)]

Method #1 : Using enumerate() + list comprehension + isalpha() The combination of above functions can be used to solve this problem. In this, we perform the task of working with indices using enumerate and characters filtering is done using isalpha(). 






# Python3 code to demonstrate working of
# Character coordinates in Matrix
# Using enumerate() + list comprehension + isalpha()
         
# initializing list
test_list = ['23f45.;4d', '5678d56d', '789', '5678g']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Character coordinates in Matrix
# Using enumerate() + list comprehension + isalpha()
res = [(x, y) for x, val in enumerate(test_list)
        for y, chr in enumerate(val) if chr.isalpha()] 
 
# printing result
print("Character indices : " + str(res))

Output : 
The original list is : ['23f45.;4d', '5678d56d', '789', '5678g']
Character indices : [(0, 2), (0, 8), (1, 4), (1, 7), (3, 4)]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  enumerate() + list comprehension + isalpha() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

Method #2 : Using regex() + loop The combination of above functions can be used to solve this problem. In this, we perform task of filtering alphabets using regex expressions. Just returns the first occurrence of character in each string. 




# Python3 code to demonstrate working of
# Character coordinates in Matrix
# Using regex() + loop
import re
         
# initializing list
test_list = ['23f45.;4d', '5678d56d', '789', '5678g']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Character coordinates in Matrix
# Using regex() + loop
res = []
for key, val in enumerate(test_list):
    temp = re.search('([a-zA-Z]+)', val)
    if temp :
        res.append((key, temp.start()))
         
# printing result
print("Character indices : " + str(res))

Output : 
The original list is : ['23f45.;4d', '5678d56d', '789', '5678g']
Character indices : [(0, 2), (1, 4), (3, 4)]

Method 3: Using itertools.product()

In this method, we can use the itertools.product() method to generate all possible (x,y) coordinates for the matrix. We can then use list comprehension to filter out the coordinates where the corresponding character is an alphabet.




# Python3 code to demonstrate working of
# Character coordinates in Matrix
# Using itertools.product()
 
from itertools import product
 
# initializing list
test_list = ['23f45.;4d', '5678d56d', '789', '5678g']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Character coordinates in Matrix
# Using itertools.product()
n = max(len(s) for s in test_list)
res = [(x, y) for x, y in product(range(len(test_list)), range(n))
       if y < len(test_list[x]) and test_list[x][y].isalpha()]
 
# printing result
print("Character indices : " + str(res))

Output
The original list is : ['23f45.;4d', '5678d56d', '789', '5678g']
Character indices : [(0, 2), (0, 8), (1, 4), (1, 7), (3, 4)]

Time complexity: O(mn) where m is the number of strings and n is the maximum length of the strings.
Auxiliary space: O(1) since we’re not using any extra data structures.

Method #4: Using map() and filter()

Use map() and filter() functions to solve this problem. map() is a built-in Python function that applies a given function to each element of an iterable and returns an iterator with the results. filter() is another built-in Python function that filters out elements from an iterable that do not satisfy a given condition. 

Step-by-step approach:




# Python3 code to demonstrate working of
# Character coordinates in Matrix
# Using map() and filter()
 
from itertools import product
 
# initializing list
test_list = ['23f45.;4d', '5678d56d', '789', '5678g']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Character coordinates in Matrix
# Using map() and filter()
res = list(filter(lambda x: x[1] < len(test_list[x[0]]) and test_list[x[0]][x[1]].isalpha(),
                  map(lambda x: (x[0], x[1]), product(range(len(test_list)), range(max(len(s) for s in test_list))))))
 
# printing result
print("Character indices : " + str(res))

Output
The original list is : ['23f45.;4d', '5678d56d', '789', '5678g']
Character indices : [(0, 2), (0, 8), (1, 4), (1, 7), (3, 4)]

Time complexity: O(n^2), where n is the length of the longest string in the list.
Auxiliary space: O(n^2), where n is the length of the longest string in the list (for creating the Cartesian product).


Article Tags :