In this Python tutorial, we will discuss different ways to get string values from a list in Python. And for this topic, let’s understand the following scenario
How to get string values from list in Python: Scenario
- In this scenario, we have created a excel sheet which contains two columns Column A and column B. In these columns, there are two major attributes ‘Verbatim‘ and ‘Keyword’. In Verbatim(ColumnA) we have assigned some phrases) and other side we have selected some words in Keyword attribute.
- First scenario: We have to find common words of ColumnB in Column A. For example suppose you have two given arrays the array1 is containing ‘verbatim‘ (column A) elements and the array2 elements contains ‘Keywords‘ elements. Now we have to find the common words between both the arrays
- Second scenario: One word from column A must be attached with column B keywords.
There are various methods for finding the value of Column A in Column B. let’s have a look at the methods.
- By using the regex method
- By using Numpy method
- By using for-loop method
By using NumPy method
- In this Program, we will learn how to find the common values from input arrays by using the NumPy method.
- To perform this particular task we are going to use the np.intersect1d() function. In Python, this function is used to find the intersection between two input arrays and this method takes two arrays as an argument and it will check the condition if the value of array 1 exists in array2 or not. In simple words, the intersection is defined as getting unique or common values from both the input arrays.
Here is the Syntax of Python np.intersect1d() function.
numpy.intersect1d ( ar1, ar2, assume_unique=False, return_indices=False )
import numpy as np x = np.array(['Books','open','doors','in','our','minds']) y= np.array(['open','doors','minds']) result = np.intersect1d(x,y) print(result)
In the following given code we have imported the NumPy library and then created an array by using the np.array() function and within this function, we have assigned the string characters.
After creating an array we have declared a variable ‘result’ and used the np.intersect1d() function and within this function, we have assigned the input arrays as an argument.
Here is the implementation of the following given code.
By using the regex method
- In this section, we will learn how to get the string values from list by using the Python regex method.
- In Python, the regex method is used for describing a search pattern in the given input text, and to create a regex object we are going to import the ‘re’ module.
- After importing the module, we will use the re.search() method and within this function, we will assign the input text and this function will help the user to compile a regular expression and also search a pattern.
import re input = re.compile(r'(Books|teach|us|about|love|heartbreak)') result = input.search('emotions heartbreak') print('love and '+result.group())
In the above code, we have used the re.compile() method and this function takes input as a regular expression input and will check the same pattern inside the text. After that we have used the re.search() method and this method will help the user to find the occurrence of the pattern in the given string.
Here is the Screenshot of the following given code.
Read How to split a string using regex in python
By using for-loop method
In this section, we will learn how to find the value of Column A in Column B by using the for-loop method.
column_A = "A lesson for life from one lion to another" column_B = ['lesson'] for i in column_B: if i in column_A: print(i)
In the above code we have created an input string ‘column_A’ along with that we created a list in which we have assigned the words which we want to operate with the input string.
In the given example, we have to find the value of column_B elements in column_A elements. To do this task we are going to use the for-loop method and iterate the values.
Here is the Output of the following given code.
In this Python tutorial, we have learned how to get string values from a list in Python.
Related Python tutorials:
- Python find index of element in list
- How to get the index of an element in Python List
- Add Elements in List in Python Using For Loop
- Python find number in String
- Python NumPy Median
- Python Copy NumPy Array
- Python NumPy genfromtxt()
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.