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 20:24:42 +0100

anonymous user

Anonymous

updated 2018-10-12 01:56:53 +0100

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 20:02:22 +0100

tmonteil gravatar image

updated 2018-10-12 22:36:10 +0100

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 21:50:19 +0100 )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 22:36:55 +0100 )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 22:55:19 +0100 )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-13 00:59:05 +0100 )edit

You can use the flatten function.

tmonteil gravatar imagetmonteil ( 2018-10-13 11:14:30 +0100 )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 20:16:34 +0100

Seen: 330 times

Last updated: Oct 12 '18