[英]Python: Parameters to Run the Same Function Multiple Times
I am studying rotating a list, and made a function to rotate the list left and right, but how can I write a code for how many times to rotate?我正在研究旋转列表,并制作了一个左右旋转列表的功能,但是如何编写旋转多少次的代码? if that makes a sense.
如果这是有道理的。 I want to pass it as an argument to the functions.
我想将它作为参数传递给函数。
table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]
def rotate_left():
(table.append(table.pop(0)))
return table
print(rotate_left())
def rotate_right():
(table.insert(0,table.pop()))
return table
print(rotate_right())
You can use for loop
inside your functions and pass how many times you want to rotate as a argument.您可以在函数中使用
for loop
,并传递要旋转的次数作为参数。
table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]
def rotate_left(rotate_times):
for _ in range(rotate_times):
table.append(table.pop(0))
return table
>>> print(rotate_left(2))
>>> [20, 0, 59, 86, 32, 11, 9, 40, 1, 10]
def rotate_right(rotate_times):
for _ in range(rotate_times):
table.insert(0,table.pop())
return table
>>> print(rotate_right(2))
>>> [1, 10, 20, 0, 59, 86, 32, 11, 9, 40]
NOTE笔记
In above scenario, be aware of the fact that, when you pass a list
to a method and modify it inside that method, the changes are made in original list
unless you make a deep copy
, because list
is a mutable
type.在上述场景中,请注意,当您将
list
传递给方法并在该方法内对其进行修改时,除非您进行deep copy
,否则更改将在original list
进行,因为list
是mutable
类型。
So, when you call rotate_left(2)
, it rotates the original list
twice towards left.因此,当您调用
rotate_left(2)
时,它会将original list
向左旋转两次。 Then when you call rotate_right(2)
, it rotates the original list
, which is already rotated by rotate_left(2)
, so we got the list as in initial order.然后,当您调用
rotate_right(2)
时,它会旋转已由rotate_left(2)
(2) 旋转的original list
,因此我们按初始顺序获得列表。
As, the functions are already modifying the original list
, you can remove return table
from the function (unless you want a new deep copy of list).由于函数已经在修改
original list
,您可以从函数中删除return table
(除非您想要新的列表深层副本)。 And simply print the list after that like:然后简单地打印列表,如:
def rotate_left(rotate_times):
for _ in range(rotate_times):
table.append(table.pop(0))
>>> rotate_left(2)
>>> print(table)
>>> [20, 0, 59, 86, 32, 11, 9, 40, 1, 10]
You can write a 'for loop' and use 'range' to decide how many times you want to rotate.您可以编写一个“for 循环”并使用“范围”来决定要旋转多少次。 In this example 'rotate_left()' is called three times:
在此示例中,“rotate_left()”被调用了 3 次:
table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]
def rotate_left():
(table.append(table.pop(0)))
return table
def rotate_right():
(table.insert(0,table.pop()))
return table
for i in range(3):
print(rotate_left())
You can write a ' loop ' and use ' range ' to decide how many times you want to loop.您可以编写一个“循环”并使用“范围”来决定要循环多少次。 In this example, there is a program that asks the user in which direction and how many times to turn and calculates.
在这个例子中,有一个程序询问用户在哪个方向和多少次转动和计算。
def rotate_left(count,table):
for i in range (count):
(table.append(table.pop(0)))
return table
def rotate_right(count,table):
for i in range (count):
(table.insert(0,table.pop()))
return table
def main():
table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]
isSelect = True
while(isSelect):
rotate = int(input("1- Rotate Left\n2- Rotate Right\n: "))
count = int(input("\nHow many times to rotate ?\n: "))
if((rotate == 1 or rotate == 2) and count > 0):
isSelect = False
if (rotate == 1):
print(rotate_left(count,table))
elif (rotate == 2):
print(rotate_right(count,table))
else:
print("\nInvalid Parameter. Please choose again.\n")
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.