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

  • SEARCH
  • Home
  • 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 8732577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:27:51+00:00 2026-06-13T09:27:51+00:00

I have a string, dictionary in the form: (‘(Laughter flower)’, {‘laughter’: (8.5, 0.9313), ‘flower’:

  • 0

I have a string, dictionary in the form:

('(Laughter 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)
 }
)

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 ('(Laughter 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 'laughter' and 'flower': 8.5 + 7.88 / 2 but this running function also includes 'laugh' and 'flow' : 8.5 + 7.88 + 9.5 + 2.3 /4.

  • 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-13T09:27:52+00:00Added an answer on June 13, 2026 at 9:27 am

    @Ignaco is right about why you’re including “flow” and “laugh”…

    You could write the code as the following though:

    data = ('(Laughter 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)})
    
    # Unpack for naming
    keys, vals = data
    # Assume () and first and last
    look_for = keys[1:-1].lower().split()
    # Get relevant numbers
    nums = [vals[k][0] for k in look_for]
    # Print average
    print sum(nums) / len(nums)
    

    so you generalise the function to just average the first element of relevant keys:

    def somefunc(keys, dct):
        vals = [dct[k][0] for k in keys]
        return sum(vals) / float(len(vals))
    

    And you have to pre-process some string somehow, so that it’s a sequence of valid keys:

    some_string = '(laughter flower)'
    keys = some_string[1:-1].lower().split()
    print somefunc(keys, some_dict)
    
    • 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: ('The puppy likes flowers', {'laughter': (8.5,
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.