0

I'm having a simple problem using Python functions.

I'll try to be clear:

I have to write two functions:

The first one, returns a dictionary where the keys are names, associated to lists of 5 random numbers.

{'John': [25, 27, 30, 14, 15], 'Mary': [15, 26, 14, 31, 12], 'Saray': [27, 15, 19, 14, 39]}

This is an example of the output of the first function.

The second function, should return me a dictionary with the same keys, and the average of the five numbers of the lists. expected output:

{'John': [21.8], 'Mary': [19.6], 'Saray': [22.8]}

The problem is that, I don't know how the second function should take as input the results of the first one. I tried with something like that:

def First_Function(a):
    d={}
    [...]
    x=list(d.keys())
    y=list(d.values())
    return d

def Second_Function(a=x,b=y):

But the progrm says that x and y are not defined. What can i do?

3
  • Please update your question with code indicating how you expect to call these functions. Commented Jan 29, 2020 at 12:05
  • it's written in the last part of the code, in "ex2" @quamrana Commented Jan 29, 2020 at 12:07
  • You have shown some possible definitions of functions, but not how they are called. Please update your question. Commented Jan 29, 2020 at 12:09

3 Answers 3

2

x and y are valid variables only inside the first function.

Your first function is returning dictionary, pass that into second one.

Example:

def ex2(d):
    x = list(d.keys())
    y = list(d.values())
    # What ever your function does.

result = ex1(a)
ex2(d=result)  # I gave myself a freedom to name parameter d
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you have dictionary generated by the first function

 import numpy as np

 def func_(a):
     for key, value in a.items():
        a[key] = [np.mean(a[key])]
     return a

func_(a)
#op
{'John': [22.2], 'Mary': [19.6], 'Saray': [22.8]}

Comments

0

If you're writing two separate functions. that is not nesting.

With two separate functions, this would be a valid way to do it:

def first_function(a):
    d={}
    [...]
    return d

def second_function (d: dict):
    result = {}
    for key in d:
        my_list = d[key]
        result[key] = sum(my_list)/len(my_list)
    return result

Output:

>>> second_function({'John': [25, 27, 30, 14, 15], 'Mary': [15, 26, 14, 31, 12], 'Saray': [27, 15, 19, 14, 39]})

{'John': 22.2, 'Mary': 19.6, 'Saray': 22.8}

You may want to modify the code to suit your expected output. However, I couldn't help but notice the average calculated for the "John" key in your question is wrong.

To nest the second function into the first one and have it be executed as a part of the first function, you can use this syntax:

def first_function(a):

    def second_function (d: dict):
        result = {}
        for key in d:
            my_list = d[key]
            result[key] = sum(my_list)/len(my_list)
        return result

    d={}
    [...]
    return second_function(d)

2 Comments

and so in the second function I have to copy the output of the first one? there is no way to do that automatically?
@1mari I have edited my answer so that it answers your comment. See if it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.