Ask Your Question
0

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

asked 6 years ago

anonymous user

Anonymous

updated 6 years ago

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?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 6 years ago

tmonteil gravatar image

updated 6 years ago

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
Preview: (hide)
link

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 ( 6 years ago )

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

tmonteil gravatar imagetmonteil ( 6 years ago )

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 ( 6 years ago )

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 ( 6 years ago )

You can use the flatten function.

tmonteil gravatar imagetmonteil ( 6 years ago )

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: 6 years ago

Seen: 344 times

Last updated: Oct 12 '18