Check whether "1" list value contains "2" list

Asked

Viewed 3,063 times

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 the values of track_list appear in tweets_data, if they appear, print on the screen such values.

I would also like to know how to obtain the string most repeated in the lists.

3 answers

4

you can do so:

    #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)

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 to do this with python. This I consider the simplest to understand.

Example: https://repl.it/BRuh/1

  • I performed this configuration as informed but did not get to print value, any, could show you more in depth my code?

3


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.