如何在函数中使用* args和** kargs在Python中传递嵌套列表,元组,集合和字典? [英] How can I pass nested list, tuple, set and dictionary in Python using *args and **kargs in function?

查看:469
本文介绍了如何在函数中使用* args和** kargs在Python中传递嵌套列表,元组,集合和字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def arithmetic_mean(first,* values):

此函数计算非空的算术平均值
任意数量的数值



返回(第一+总和(值))/(1 + len(值))



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

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



我有什么尝试过:



可以通过zip方式或其他任何方式实现吗?

解决方案

< blockquote>应该可以,请参阅 https://docs.python.org/3.4/tutorial/ controlflow.html#keyword-arguments [ ^ ]。

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)]]

What I have tried:

can it be possible by zip method or any other way possible?

解决方案

It should be possible, see https://docs.python.org/3.4/tutorial/controlflow.html#keyword-arguments[^].


这篇关于如何在函数中使用* args和** kargs在Python中传递嵌套列表,元组,集合和字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆