以字典作为可选参数的函数 - Python
Function with dictionaries as optional arguments - Python
我正在尝试创建一个函数,它可以接收许多或一些词典作为输入。我正在使用以下代码:
def merge_many_dics(dic1,dic2,dic3=True,dic4=True,dic5=True,dic6=True,dic7=True,dic8=True,dic9=True,dic10=True):
"""
Merging up to 10 dictionaries with same keys and different values
:return: a dictionary containing the common dates as keys and both values as values
"""
manydics = {}
for k in dic1.viewkeys() & dic2.viewkeys() & dic3.viewkeys() & dic4.viewkeys() & dic5.viewkeys() & dic6.viewkeys()\
& dic7.viewkeys() & dic8.viewkeys() & dic9.viewkeys() & dic10.viewkeys():
manydics[k] = (dic1[k], dic2[k],dic3[k],dic4[k],dic5[k],dic6[k],dic7[k],dic8[k],dic9[k],dic10[k])
return manydics
请注意,我试图使参数 dic3、dic4、dic5 等等于 "True",因此当它们未指定并在函数中被调用时,不会发生任何事情。但是我收到以下错误:
Traceback (most recent call last):
File "/Users/File.py", line 616, in <module>
main_dic=merge_many_dics(dic1,dic2,dic3,dic4)
File "/Users/File.py", line 132, in merge_many_dics
& dic7.viewkeys() & dic8.viewkeys() & dic9.viewkeys() & dic10.viewkeys():
AttributeError: 'bool' object has no attribute 'viewkeys'
有没有人可以指导我的旅程?
您应该尝试 args 语法:
def merge_many_dics(*args):
iterate over your args to join them
然后您可以使用任意数量的参数调用该函数。
带有 *args 的函数可以如下所示:
def print_all(name, *args):
print "Hello", name, "here are your args"
for arg in args:
print arg
print_all("Claus", "car", "boat", "house")
这将打印:
Hello Clause here are your args
car
boat
house
如错误所述,您无法查看布尔值的键,也就是 True.viewkeys() 不起作用。将默认字典更改为空,留下:
def merge_many_dics(dic1,dic2,dic3={},dic4={},dic5={},dic6={},dic7={},dic8={},dic9={},dic10={}):
"""
Merging up to 10 dictionaries with same keys and different values
:return: a dictionary containing the common dates as keys and both values as values
"""
manydics = {}
for k in dic1.viewkeys() & dic2.viewkeys() & dic3.viewkeys() & dic4.viewkeys() & dic5.viewkeys() & dic6.viewkeys()\
& dic7.viewkeys() & dic8.viewkeys() & dic9.viewkeys() & dic10.viewkeys():
manydics[k] = (dic1[k], dic2[k],dic3[k],dic4[k],dic5[k],dic6[k],dic7[k],dic8[k],dic9[k],dic10[k])
return manydics
这是一个实现,其中您为每个键创建一个列表并添加每个项目,我相信它可以进一步优化,但它非常可读:
def merge_many_dicts(*args):
"""
Merging all dictionaries suposing all dictionaries share keys
:return: a dictionary containing the common dates as keys and both values as values
"""
manydicts = {}
for k in args:
for key in k.iterkeys():
manydicts[key] = []
for k in args:
for key, value in k.iteritems():
manydicts[key].append(value)
return manydicts
使用arbitrary argument list,可以使用任意数量的参数调用函数:
>>> def merge_many_dics(*dicts):
... common_keys = reduce(lambda a, b: a & b, (d.viewkeys() for d in dicts))
... return {key: tuple(d[key] for d in dicts) for key in common_keys}
...
>>> merge_many_dics({1:2}, {1:3}, {1:4, 2:5})
{1: (2, 3, 4)}
您可以使用以下代码,在函数中初始化这些参数
def merge_many_dics(dic1, dic2, dic3=None, dic4=None, dic5=None, dic6=None, dic7=None, dic8=None, dic9=None,
dic10=None):
"""
Merging up to 10 dictionaries with same keys and different values
:return: a dictionary containing the common dates as keys and both values as values
"""
for item in locals().items():
if item is None:
item = dic1
manydics = {}
for k in dic1.viewkeys() & dic2.viewkeys() & dic3.viewkeys() & dic4.viewkeys() & dic5.viewkeys() & dic6.viewkeys() \
& dic7.viewkeys() & dic8.viewkeys() & dic9.viewkeys() & dic10.viewkeys():
manydics[k] = (dic1[k], dic2[k], dic3[k], dic4[k], dic5[k], dic6[k], dic7[k], dic8[k], dic9[k], dic10[k])
return manydics
这是Python 3.x 基于@falsetru 答案并使用operator.and_
函数的答案。
>>> from functools import reduce
>>> import operator
>>> def merge_many_dicts(*dicts):
... common_keys = reduce(operator.and_, (d.keys() for d in dicts))
... return {key: tuple(d[key] for d in dicts) for key in common_keys}
...
>>> merge_many_dicts({1:2}, {1:3}, {1:4, 2:5})
{1: (2, 3, 4)}
我正在尝试创建一个函数,它可以接收许多或一些词典作为输入。我正在使用以下代码:
def merge_many_dics(dic1,dic2,dic3=True,dic4=True,dic5=True,dic6=True,dic7=True,dic8=True,dic9=True,dic10=True):
"""
Merging up to 10 dictionaries with same keys and different values
:return: a dictionary containing the common dates as keys and both values as values
"""
manydics = {}
for k in dic1.viewkeys() & dic2.viewkeys() & dic3.viewkeys() & dic4.viewkeys() & dic5.viewkeys() & dic6.viewkeys()\
& dic7.viewkeys() & dic8.viewkeys() & dic9.viewkeys() & dic10.viewkeys():
manydics[k] = (dic1[k], dic2[k],dic3[k],dic4[k],dic5[k],dic6[k],dic7[k],dic8[k],dic9[k],dic10[k])
return manydics
请注意,我试图使参数 dic3、dic4、dic5 等等于 "True",因此当它们未指定并在函数中被调用时,不会发生任何事情。但是我收到以下错误:
Traceback (most recent call last):
File "/Users/File.py", line 616, in <module>
main_dic=merge_many_dics(dic1,dic2,dic3,dic4)
File "/Users/File.py", line 132, in merge_many_dics
& dic7.viewkeys() & dic8.viewkeys() & dic9.viewkeys() & dic10.viewkeys():
AttributeError: 'bool' object has no attribute 'viewkeys'
有没有人可以指导我的旅程?
您应该尝试 args 语法:
def merge_many_dics(*args):
iterate over your args to join them
然后您可以使用任意数量的参数调用该函数。
带有 *args 的函数可以如下所示:
def print_all(name, *args):
print "Hello", name, "here are your args"
for arg in args:
print arg
print_all("Claus", "car", "boat", "house")
这将打印:
Hello Clause here are your args
car
boat
house
如错误所述,您无法查看布尔值的键,也就是 True.viewkeys() 不起作用。将默认字典更改为空,留下:
def merge_many_dics(dic1,dic2,dic3={},dic4={},dic5={},dic6={},dic7={},dic8={},dic9={},dic10={}):
"""
Merging up to 10 dictionaries with same keys and different values
:return: a dictionary containing the common dates as keys and both values as values
"""
manydics = {}
for k in dic1.viewkeys() & dic2.viewkeys() & dic3.viewkeys() & dic4.viewkeys() & dic5.viewkeys() & dic6.viewkeys()\
& dic7.viewkeys() & dic8.viewkeys() & dic9.viewkeys() & dic10.viewkeys():
manydics[k] = (dic1[k], dic2[k],dic3[k],dic4[k],dic5[k],dic6[k],dic7[k],dic8[k],dic9[k],dic10[k])
return manydics
这是一个实现,其中您为每个键创建一个列表并添加每个项目,我相信它可以进一步优化,但它非常可读:
def merge_many_dicts(*args):
"""
Merging all dictionaries suposing all dictionaries share keys
:return: a dictionary containing the common dates as keys and both values as values
"""
manydicts = {}
for k in args:
for key in k.iterkeys():
manydicts[key] = []
for k in args:
for key, value in k.iteritems():
manydicts[key].append(value)
return manydicts
使用arbitrary argument list,可以使用任意数量的参数调用函数:
>>> def merge_many_dics(*dicts):
... common_keys = reduce(lambda a, b: a & b, (d.viewkeys() for d in dicts))
... return {key: tuple(d[key] for d in dicts) for key in common_keys}
...
>>> merge_many_dics({1:2}, {1:3}, {1:4, 2:5})
{1: (2, 3, 4)}
您可以使用以下代码,在函数中初始化这些参数
def merge_many_dics(dic1, dic2, dic3=None, dic4=None, dic5=None, dic6=None, dic7=None, dic8=None, dic9=None,
dic10=None):
"""
Merging up to 10 dictionaries with same keys and different values
:return: a dictionary containing the common dates as keys and both values as values
"""
for item in locals().items():
if item is None:
item = dic1
manydics = {}
for k in dic1.viewkeys() & dic2.viewkeys() & dic3.viewkeys() & dic4.viewkeys() & dic5.viewkeys() & dic6.viewkeys() \
& dic7.viewkeys() & dic8.viewkeys() & dic9.viewkeys() & dic10.viewkeys():
manydics[k] = (dic1[k], dic2[k], dic3[k], dic4[k], dic5[k], dic6[k], dic7[k], dic8[k], dic9[k], dic10[k])
return manydics
这是Python 3.x 基于@falsetru 答案并使用operator.and_
函数的答案。
>>> from functools import reduce
>>> import operator
>>> def merge_many_dicts(*dicts):
... common_keys = reduce(operator.and_, (d.keys() for d in dicts))
... return {key: tuple(d[key] for d in dicts) for key in common_keys}
...
>>> merge_many_dicts({1:2}, {1:3}, {1:4, 2:5})
{1: (2, 3, 4)}