Like mentioned by Dherik, there are other ways to do this in Python, one of them is to use the function itertools.product
which is equivalent to nested loops, see an 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)
See the demonstração
Another way is compress the 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']
See the demonstração
To know which word appears most in a string you can use the function 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
I performed this configuration as informed but did not get to print value, any, could show you more in depth my code?
– Lucas Silva Simão