Score each word from list variable - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Score each word from list variable (/thread-19067.html) |
Score each word from list variable - pythonias2019 - Jun-12-2019 Hi, I have a list comprised of a tweet and score. I'd like to split the tweet into words and assign the total score of the tweet to each word. Any help is much appreciated, new to Python! Thanks Tweet_list = [('You better believe you're beautiful', 3),('Happy Birthday to my friends',4)] def word_sentiment(): list_words = [] for tweet in tweet_list: for word in tweet.split(" "): list_words.append((word)) return list_words Desired output:
RE: Score each word from list variable - gontajones - Jun-12-2019 def word_sentiment(tuple_list): list_words = [] # Loop through each tuple (tp) for tp in tuple_list: for word in tp[0].split(" "): list_words.append((word, tp[1])) return list_words tweet_list = [("You better believe you're beautiful", 3),("Happy Birthday to my friends",4)] my_final_list = word_sentiment(tweet_list) print(my_final_list) RE: Score each word from list variable - perfringo - Jun-12-2019 I observe ambiguity in "split the tweet into words and assign the total score of the tweet to each word". My house, my rules. -> 'My' 'house,' 'my' 'rules.' my house my rules -> 'my' 'house' 'my' 'rules' Aren't 'my' and 'My' same words? Aren't 'rules.' and 'rules' also? RE: Score each word from list variable - pythonias2019 - Jun-13-2019 (Jun-12-2019, 12:07 PM)gontajones Wrote:def word_sentiment(tuple_list): list_words = [] # Loop through each tuple (tp) for tp in tuple_list: for word in tp[0].split(" "): list_words.append((word, tp[1])) return list_words tweet_list = [("You better believe you're beautiful", 3),("Happy Birthday to my friends",4)] my_final_list = word_sentiment(tweet_list) print(my_final_list) RE: Score each word from list variable - stullis - Jun-13-2019 def word_sentiment(tuple_list): # Loop through each tuple (tp) return [[(word, tp[1]) for word in tp[0].split(" ")] for tp in tuple_list] RE: Score each word from list variable - pythonias2019 - Jun-13-2019 Thanks gontajones! I have another related to this...how would I remove a word from the tuple if it's in dictionary? scores= { beautiful,3 friends= 4} def word_sentiment(tuple_list): list_words = [] # Loop through each tuple (tp) for tp in tuple_list: for word in tp[0].split(" "): list_words.append((word, tp[1])) return list_words tweet_list = [("You better believe you're beautiful", 3),("Happy Birthday to my friends",4)] my_final_list = word_sentiment(tweet_list) print(my_final_list)Desired Output:
RE: Score each word from list variable - gontajones - Jun-13-2019 This time I'll let you try... You can use something like: if word not in not_allowed_words: ... |