说明:key – 字典中要查找的键。default – 如果指定键的值不存在时,返回该默认值值。
程序如下:
def multiple(arg, *args):
print("arg: ", arg)
for value in args:
print("other args:", value)
def multiple2(**args):
for key in args:
print(key + ":", str(args[key]))
# print(key + ":" + bytes(args[key]))
def multiple3(arg, *args, **dictargs):
print("arg: ", arg )
for value in args:
print("other args:", value)
for key in dictargs:
print("dictargs:" + key + ":" + str(dictargs[key]))
# print("dictargs:" + key + ":" + bytes(dictargs[key]))
if __name__ == '__main__':
print("++++++++++++")
multiple(1,'a',True, 13212, "asdfasf ", "asdfas df", {"aaa":"asdf ", 123:123, })
print("++++++++++++")
multiple2(name='Amy', age=12, single=True)
print("++++++++++++")
multiple3(1,'a',True, name='Amy',age=12, )
print
结果如下:
++++++++++++
arg: 1
other args: a
other args: True
other args: 13212
other args: asdfasf
other args: asdfas df
other args: {'aaa': 'asdf ', 123: 123}
++++++++++++
name: Amy
age: 12
single: True
++++++++++++
arg: 1
other args: a
other args: True
dictargs:name:Amy
dictargs:age:12
程序如下:
def add(x, y):
return x + y
if __name__ == "__main__":
para = (1,2)
print([*para]) # [1, 2]
para2 = [1, 3]
print([*para2]) # [1, 3]
para3 = {"x":2, "y":3}
print([*para3]) # ['x', 'y']
# print([**para2]) # SyntaxError: invalid syntax
print(add(*para)) # 3
print(add(*para2)) # 4
print(add(**para3)) # 5
a = [1 ,2,3]
a += [5, 6, 7, 8]
print(a) # [1, 2, 3, 5, 6, 7, 8]
a = {'loss': 75.91, 'x': 0.11}
print("a.items() = ", a.items())
for name, metric in a.items():
print(name, metric)
显示结果如下:
a.items() = dict_items([('loss', 75.91), ('x', 0.11)])
loss 75.91
x 0.11