Sometimes while working with data, we can receive separate data in the form of strings and we need to compile them into Matrix for its further use. This can have applications in many domains. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + split() + zip()
The combination of the above functions can be used to perform this task. In this, we combine strings into Matrix row elements using zip() and split performs the task of extracting word from strings.
# Python3 code to demonstrate working of # Combine Strings to Matrix # Using list comprehension + zip() + split() # initializing strings test_str1 = "Gfg is best"test_str2 = "1 2 3" # printing original strings print("The original string 1 is : " + test_str1) print("The original string 2 is : " + test_str2) # Combine Strings to Matrix # Using list comprehension + zip() + split() res = [[idx, int(j)] for idx, j in zip(test_str1.split(' '), test_str2.split(' '))] # printing result print("Does Matrix after construction : " + str(res)) |
The original list 1 is : [‘Gfg’, ‘is’, ‘best’]
The original list 2 is : [[‘Gfg’, 1], [‘is’, 2], [‘best’, 1], [‘Gfg’, 4], [‘is’, 8], [‘Gfg’, 7]]
The dictionary after grouping : {‘is’: [2, 8], ‘Gfg’: [1, 4, 7], ‘best’: [1]}
Method #2 : Using map() + split() + zip()
The combination of above functions can be used to perform this task. This performs in similar way as above. The difference is that the logic of extension is done using map().
# Python3 code to demonstrate working of # Combine Strings to Matrix # Using map() + zip() + split() # initializing strings test_str1 = "Gfg is best"test_str2 = "1 2 3" # printing original strings print("The original string 1 is : " + test_str1) print("The original string 2 is : " + test_str2) # Combine Strings to Matrix # Using map() + zip() + split() res = list(map(list, zip(test_str1.split(' '), map(int, test_str2.split(' '))))) # printing result print("Does Matrix after construction : " + str(res)) |
The original list 1 is : [‘Gfg’, ‘is’, ‘best’]
The original list 2 is : [[‘Gfg’, 1], [‘is’, 2], [‘best’, 1], [‘Gfg’, 4], [‘is’, 8], [‘Gfg’, 7]]
The dictionary after grouping : {‘is’: [2, 8], ‘Gfg’: [1, 4, 7], ‘best’: [1]}
Recommended Posts:
- Python | Pair and combine nested list to tuple list
- Python | Pandas Series.combine()
- Python | Pandas Series.combine()
- Python | Combine the values of two dictionaries having same key
- Python | Combine two lists by maintaining duplicates in first list
- Python | Combine two dictionary adding values for common keys
- Python - Combine dictionary with priority
- Combine keys in a list of dictionaries in Python
- Combine similar characters in Python using Dictionary Get() Method
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python - Combine list with other list elements
- NLP | Backoff Tagging to combine taggers
- How to combine Groupby and Multiple Aggregate Functions in Pandas?
- Combine two Pandas series into a DataFrame
- Combine Multiple Excel Worksheets Into a Single Pandas Dataframe
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Python - List Strings frequency in Matrix
- Python - Convert Strings to Character Matrix
- Python program to Convert a Matrix to Sparse Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Improved By : nidhi_biet

