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().
The original list is : ['23f45.;4d', '5678d56d', '789', '5678g'] Character indices : [(0, 2), (0, 8), (1, 4), (1, 7), (3, 4)]
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.
The original list is : ['23f45.;4d', '5678d56d', '789', '5678g'] Character indices : [(0, 2), (1, 4), (3, 4)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Calculate geographic coordinates of places using google geocoding API
- Python | Reverse Geocoding to get location on a map using geographic coordinates
- Python | Convert location coordinates to tuple
- Python - Adjacent Coordinates in N dimension
- Python - Group Adjacent Coordinates
- Displaying the coordinates of the points clicked on the image using Python-OpenCV
- Parallel Coordinates Plot using Plotly in Python
- Python Program to convert complex numbers to Polar coordinates
- PyQt5 – How to get Label coordinates ?
- Python | Insert character after every character pair
- Python program to read character by character from a file
- Python - Create a Dictionary with Key as First Character and Value as Words Starting with that Character
- Python - Convert Strings to Character Matrix
- Python | String List to Column Character Matrix
- Python | Convert Character Matrix to single String
- Python program to Convert a Matrix to Sparse Matrix
- Python - Convert Matrix to Custom Tuple Matrix
- Python - Convert Integer Matrix to String Matrix
- Convert covariance matrix to correlation matrix using Python
- Create an n x n square matrix, where all the sub-matrix have the sum of opposite corner elements as even
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.