Ask Your Question
0

How to call nested tuple and nested set or dictionary using variable argument and variable keyword argument methods ?

asked 2018-10-11 13:24:42 -0500

anonymous user

Anonymous

updated 2018-10-11 18:56:53 -0500

tmonteil gravatar image

.

def arithmetic_mean(first, *values):
        """ This function calculates the arithmetic mean of a non-empty
            arbitrary number of numerical values """

        return (first + sum(values)) / (1 + len(values))

    x= [('a', 232), ('b', 343), ('c', 543), ('d', 23)]
    y= [[('a', 232), ('b', 343), ('c', 543), ('d', 23)]]

how to pass x and y inside arithmetic_mean. can it be possible through zip method?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-10-12 13:02:22 -0500

tmonteil gravatar image

updated 2018-10-12 15:36:10 -0500

I am not sure about your exact question, does the following answer it ?

sage: arithmetic_mean(*dict(x).values())
1141/4
sage: arithmetic_mean(*dict(y[0]).values())
1141/4

EDIT Here is a version using zip (though it is not very natural):

sage: arithmetic_mean(*zip(*x)[1])
1141/4
sage: arithmetic_mean(*zip(*y[0])[1])
1141/4
edit flag offensive delete link more

Comments

thanq thanq very much it solved my problem

i got information from site " https://www.python-course.eu/python3_functions.php%22 (https://www.python-course.eu/python3_...) in 'Arbitrary Number of Parameters' section that, This type of problem can be solved by zip metyhod. can u tell me, is it possible or not.

damodar gravatar imagedamodar ( 2018-10-12 14:50:19 -0500 )edit

Everything is always possible (see my edit), but the main question is which problem do you want to solve.

tmonteil gravatar imagetmonteil ( 2018-10-12 15:36:55 -0500 )edit

Thanq very much. i checked

print(arithmetic_mean(45,list(zip(x))[1])) ; print(arithmetic_mean(45,list(zip(y[0]))[1]))

it works.

damodar gravatar imagedamodar ( 2018-10-12 15:55:19 -0500 )edit

if i get values like p=[(232,), (343,), (543,), (23,)]

and

q=((232, 343, 543, 23),)

how can i will pass ?

i could not able to do without zip method, is there any other way to do?

damodar gravatar imagedamodar ( 2018-10-12 17:59:05 -0500 )edit

You can use the flatten function.

tmonteil gravatar imagetmonteil ( 2018-10-13 04:14:30 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-10-11 13:16:34 -0500

Seen: 66 times

Last updated: Oct 12