Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:46:58+00:00 2026-06-13T05:46:58+00:00

I have a string, dictionary in the form: (‘The puppy likes flowers’, {‘laughter’: (8.5,

  • 0

I have a string, dictionary in the form:

('The puppy likes flowers',
 {'laughter': (8.5, 0.9313),
  'flowers': (7.88, 1.1718),
  'the': (4.98, 0.9145),
  'puppy': (7.58, 1.4581),
  'died': (1.56, 1.198),
  'laugh': (9.5, 0.1),
  'flow': (2.3, 0.51),
  'likes':(5.9, 0.032),
  'like':(6.5, 0.021)    
   }
  )

Each parentheses is a tuple which corresponds to (score, standard deviation). I’m taking the average of just the first integer in each tuple. I’ve tried this:

def score(string, d):
    if len(string) == 0:
        return 0
    string = string.lower()
    included = [d[word][0]for word in d if word in string]
    return sum(included) / len(included)

When I run:

print score ('The puppy likes flower', {'laughter': (8.5, 0.9313), 'flower': 
(7.88, 1.1718), 'the':(4.98, 0.9145), 'puppy':(7.58, 1.4581), 
'died':(1.56, 1.198),'laugh': (9.5, 0.1),'flow': (2.3, 0.51)})

I should get the average of only 'the', 'puppy', 'likes' and 'flowers': 4.98 + 7.88 + 5.9 + 7.58 / 4 but this running function also includes 'like' and 'flow' : 4.98 + 7.88 + 5.9 + + 7.58 + 6.5 + 2.3 / 6.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:47:00+00:00Added an answer on June 13, 2026 at 5:47 am

    First off using the variable string is not a great idea … but its OK here … you have a flaw in the logic … the following works

    def avg(l):
        if l:
            return sum(l)/len(l)
        return 0
    
    def score(s, d):
        return avg([d.get(x,[0])[0] for x in s.lower().split()])
    

    This will add a 0 for pieces of the string s that are not in d … if you wanted to ignore them use the following instead

    def score(s, d):
        return avg([d[x][0] for x in s.lower().split() if x in d])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string, dictionary in the form: ('(Laughter flower)', {'laughter': (8.5, 0.9313), 'flower':
I have two complex dictionaries in the form Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> So as
I have a collection of key/values in the form of a Dictionary<string, string> .
I have the following decleration: private Dictionary<string, Dictionary<string, File>> listFiles = new Dictionary<string,Dictionary<string,File>>(); How
I have a string data which I need to parse into a dictionary object.
I have a string like string strn = abcdefghjiklmnopqrstuvwxyz and want a dictionary like:
I have a simple Dictionary(of String, Object) that I need to iterate through and
I have such dictionary Dictionary<string, object> , dictionary holds string keys and objects as
I have a Dictionary<string, List<Order>> and I want to have the list of keys
I have a dictionary public static IDictionary<string, IList<string>> checksCollection = new Dictionary<string, IList<string>>(); I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.