Check if list value "1" contains list "2"

5

I have two lists:

track_list = ['iphone 6', 'iphone 5', 'moto x2', 'galaxy s6']

tweets_data = ['Eu queria um iphone 6 sérião', 
               'nao gostei do moto x2', 
               'iphone 5 é coisa do passado']

I want to check if the track_list values appear in tweets_data , if they appear, print those values on the screen.

I would also like to know how to get the string that repeats most in the lists.

    
asked by anonymous 19.10.2015 / 21:10

3 answers

3

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

    
19.10.2015 / 22:53
3
track_list = ['iphone 6', 'iphone 5', 'moto x2', 'galaxy s6']
tweets_data = ['Eu queria um iphone 6 seriao', 'nao gostei do moto x2', 'iphone 5 e coisa do passado']

for tweet in tweets_data:
    for track in track_list:
        if track in tweet:
            print(tweet)

There are different ways of doing this with python. This I consider the simplest to understand.

Example: link

    
19.10.2015 / 21:29
3

You can do this:

    #LISTA 1
    track_list = ['iphone 6', 'iphone 5', 'moto x2', 'galaxy s6']

    #LISTA 2
    tweets_data = ['Eu queria um iphone 6 sérião', 'nao gostei do moto x2', 'iphone 5 é coisa do passado']

    for track in track_list:
        for tweet in tweets_data:
            if track in tweet:
                print(track)
    
19.10.2015 / 21:32