In: Computer Science
CALM = ['good', 'safe', 'well', 'benefit', 'ok'] And some possible worried words: WORRIED = ['bad', 'risk', 'risky', 'nervous', 'problem'] Make sure all of your words are lowercase, and that each string is a word and not a phrase -- keeps things simpler. Now weight the words from the above lists. We’ve started out with lists of calm/worried words, but some of them are stronger than others. For example, the word good is probably more positive than the word ok. Assign a weight to each word in your lists. Use positive weights for calm words and negative weights for worried words. Your lists should be 2-dimensional now, in this format: [[word, weight], [word, weight]...] Use list concatenation (+) to create one big list instead of two separate ones, something like this: ALL_WORDS = CALM + WORRIED Iterate over this new 2-dimensional list and print out each word and its corresponding weight.
(Script for ATOM or REPL to be run)
Find the code for the above question below, read the
comments provided in the code for better understanding. If found
helpful please do upvote this.
Please refer to the screenshot of the code to understand the
indentation of the code.
Code
CALM = [['good', 5], ['safe', 2], ['well', 4], ['benefit', 3], ['ok', 1]]
WORRIED = [['bad', -2], ['risk', -4], ['risky', -5], ['nervous', -1],
['problem', -3]]
BIG_LIST = CALM + WORRIED
#now iterate each item in big list
for item in BIG_LIST:
print(item[0]," : ",item[1])
Screenshot
Output