在 Python 中调用带有列表参数的函数
Calling a function with a list argument in Python
test = [
{'input':{'nums': [19, 25, 29, 3, 5, 6, 7, 9, 11, 14]}, 'output': 3},
{'input':{'nums': [6, 8, 9, 10, 11, 1, 3, 5]}, 'output': 5}
]
def count_rotations_binary(nums):
pass
如何用上面的列表调用这个函数?
您可以在调用函数时简单地提供列表名称作为实际参数。您不需要在函数声明中提供任何预期的类型。
test = [
{'input': {'nums': [19, 25, 29, 3, 5, 6, 7, 9, 11, 14]}, 'output': 3},
{'input': {'nums': [6, 8, 9, 10, 11, 1, 3, 5]}, 'output': 5}
]
# This prints whatever you give to it
def count_rotations_binary(nums):
print(nums)
# Pass the entire list
count_rotations_binary(test)
# Pass only the 'nums' value
for item in test:
count_rotations_binary(item['input']['nums'])
test = [
{'input':{'nums': [19, 25, 29, 3, 5, 6, 7, 9, 11, 14]}, 'output': 3},
{'input':{'nums': [6, 8, 9, 10, 11, 1, 3, 5]}, 'output': 5}
]
def count_rotations_binary(nums):
pass
如何用上面的列表调用这个函数?
您可以在调用函数时简单地提供列表名称作为实际参数。您不需要在函数声明中提供任何预期的类型。
test = [
{'input': {'nums': [19, 25, 29, 3, 5, 6, 7, 9, 11, 14]}, 'output': 3},
{'input': {'nums': [6, 8, 9, 10, 11, 1, 3, 5]}, 'output': 5}
]
# This prints whatever you give to it
def count_rotations_binary(nums):
print(nums)
# Pass the entire list
count_rotations_binary(test)
# Pass only the 'nums' value
for item in test:
count_rotations_binary(item['input']['nums'])