As mentioned by Dherik , there are other ways to do this in Python, one of which is to use function itertools.product
which is equivalent to nested loops , see example:
from itertools import product
track_list = ['iphone 6', 'iphone 5', 'moto x2', 'galaxy s6']
tweet_data = ['Eu queria um iphone 6 seriao',
'nao gostei do moto x2',
'iphone 5 e coisa do passado',
'abc123']
for track, tweet in product(track_list, tweet_data):
if track in tweet:
print (track)
View the demonstração
Another way is compress list :
track_list = ['iphone 6', 'iphone 5', 'moto x2', 'galaxy s6']
tweet_data = ['Eu queria um iphone 6 seriao',
'nao gostei do moto x2',
'iphone 6 e coisa do passado',
'abc123']
tracks = ([track for tweet in tweet_data for track in track_list if track in tweet])
print (tracks)
# ['iphone 6', 'moto x2', 'iphone 6']
View the demonstração
To find out which word appears the most in a string you can use most_common()
of subclass collections.Counter
, see an example:
from collections import Counter
track_list = ['iphone 6', 'iphone 5', 'moto x2', 'galaxy s6']
tweet_data = ['Eu queria um iphone 6 seriao',
'nao gostei do moto x2',
'iphone 5 e coisa do passado',
'abc123']
tracks = ([track for tweet in tweet_data for track in track_list if track in tweet])
contador = Counter(tracks).most_common()
for palavra, qtd in contador:
print ("A palavra {0} aparece {1} vez(es)".format(palavra, qtd))
# A palavra iphone 5 aparece 1 vez(es)
# A palavra iphone 6 aparece 1 vez(es)
# A palavra moto x2 aparece 1 vez(es)
See the demonstração