I am new to python and looking help to extract tags from string by string array. Let's say I have string array of ['python', 'c#', 'java', 'f#' ]
And input string of "I love Java and python".
The output should be array ['java', 'python']
Thanks for any help.
import re
stringarray = ['python', 'c#', 'core java', 'f#' ]
string = "I love Core Java and python"
pattern = '|'.join(stringarray)
output = re.findall(pattern, string.lower())
# ['core java', 'python']
stringarray = ['python', 'c#', 'core java', 'f#' ]
string = "I love Core Java and python"
output = [i for i in stringarray if i in string.lower()]
# ['core java', 'python']
stringarray = ['python', 'c#', 'java', 'f#' ]
string = "I love Java and python"
output = list(set(string.lower().split()).intersection(stringarray))
# ['java', 'python']
Short explanation: By doing string.lower().split() we split the words as lower-case in your inputstring by the default (blankspace). By converting it to a set we can access the set function intersection. Intersection will in turn find the occurences that are in both sets. Finally we wrap this around a list to get desired output. As commented by Joe Iddon this will not return repeated tags.
Are you interested in counts? Consider using collections counter and a dict comprehension:
from collections import Counter
count = {k:v for k,v in Counter(string.lower().split()).items() if k in stringarray}
print(count)
#{'java': 1, 'python': 1}
You could use the following list comprehension, which turns your string into lowercase, then iterates through each word (after using split), and returns which ones are in your array:
arr = ['python', 'c#', 'java', 'f#' ]
s = "I love Java and python"
outp = [i for i in s.lower().split() if i in arr]
>>> outp
['java', 'python']
Or you could use regular expressions:
import re
arr = ['python', 'c#', 'java', 'f#' ]
s = "I love Java and python"
outp = re.findall('|'.join(arr),s.lower())
>>> outp
['java', 'python']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With