如何找到最小值/最大值并创建列表 (python)
How to find min/ max value and create a list (python)
请忽略我未使用的导入!
我试图创建一个列表来查找 "pa_walk" 的最小值和最大值,但我只能想出如何去做,每次我尝试它时都说错误。
import random
from math import sqrt
from math import hypot
import statistics
random.seed(20190101)
def takeOnePaStep():
direction = random.randint(0,3)
if direction == 0:
return (0,1)
elif direction == 1:
return (1,0)
elif direction == 2:
return (0,-1)
elif direction == 3:
return (-1,0)
def randomWalkPa(steps):
pa = [0,0]
for _ in range (steps):
nextStep = takeOnePaStep()
pa[0] += nextStep[0]
pa[1] += nextStep[1]
pasDistance = hypot(pa[0],pa[1])
return pasDistance
# paMean = statistic.mean(distance)
steps = int(input("Please enter the number of steps: "))
tries = int(input("How many times should I perform the experiment? "))
for _ in range(tries):
pa_walk= randomWalkPa(steps)
print(pa_walk)
我猜这是因为你的函数 randomWalkPa(steps)
returns 是距离的浮点数,这就是你首先需要创建一个列表的原因(在下面的示例中,我只是 pa_walk
一个列表。在你的for循环只是.append
每次尝试到该列表的距离。最后你可以调用内置函数max()
和min()
来获得最大和最小距离。我取消了打印min 和 max 调用的命令只得到一次结果
pa_walk = []
for _ in range(tries):
pa_walk.append(randomWalkPa(steps))
print(f"The Maximum Distance reached was: {max(pa_walk)}, in trial: {pa_walk.index(max(pa_walk))}")
print(f"The Minimum Distance reached was: {min(pa_walk)}, in trial: {pa_walk.index(min(pa_walk))}")
这里是评论推荐后的完整代码(我只改了最后5行)
import random
from math import sqrt
from math import hypot
import statistics
random.seed(20190101)
def takeOnePaStep():
direction = random.randint(0,3)
if direction == 0:
return (0,1)
elif direction == 1:
return (1,0)
elif direction == 2:
return (0,-1)
elif direction == 3:
return (-1,0)
def randomWalkPa(steps):
pa = [0,0]
for _ in range (steps):
nextStep = takeOnePaStep()
pa[0] += nextStep[0]
pa[1] += nextStep[1]
pasDistance = hypot(pa[0],pa[1])
return pasDistance
# paMean = statistic.mean(distance)
steps = int(input("Please enter the number of steps: "))
tries = int(input("How many times should I perform the experiment? "))
pa_walk = []
for _ in range(tries):
pa_walk.append(randomWalkPa(steps))
print(f"The Maximum Distance reached was: {max(pa_walk)}, in trial: {pa_walk.index(max(pa_walk))}")
print(f"The Minimum Distance reached was: {min(pa_walk)}, in trial: {pa_walk.index(min(pa_walk))}")
编辑:
需要注意的一件小事,在 python 中,习惯使用下划线而不是驼峰式。这意味着函数 randomWalkPa()
最好称为 random_walk_pa()
。这不是使代码正常工作所必需的,完全取决于您
请忽略我未使用的导入!
我试图创建一个列表来查找 "pa_walk" 的最小值和最大值,但我只能想出如何去做,每次我尝试它时都说错误。
import random
from math import sqrt
from math import hypot
import statistics
random.seed(20190101)
def takeOnePaStep():
direction = random.randint(0,3)
if direction == 0:
return (0,1)
elif direction == 1:
return (1,0)
elif direction == 2:
return (0,-1)
elif direction == 3:
return (-1,0)
def randomWalkPa(steps):
pa = [0,0]
for _ in range (steps):
nextStep = takeOnePaStep()
pa[0] += nextStep[0]
pa[1] += nextStep[1]
pasDistance = hypot(pa[0],pa[1])
return pasDistance
# paMean = statistic.mean(distance)
steps = int(input("Please enter the number of steps: "))
tries = int(input("How many times should I perform the experiment? "))
for _ in range(tries):
pa_walk= randomWalkPa(steps)
print(pa_walk)
我猜这是因为你的函数 randomWalkPa(steps)
returns 是距离的浮点数,这就是你首先需要创建一个列表的原因(在下面的示例中,我只是 pa_walk
一个列表。在你的for循环只是.append
每次尝试到该列表的距离。最后你可以调用内置函数max()
和min()
来获得最大和最小距离。我取消了打印min 和 max 调用的命令只得到一次结果
pa_walk = []
for _ in range(tries):
pa_walk.append(randomWalkPa(steps))
print(f"The Maximum Distance reached was: {max(pa_walk)}, in trial: {pa_walk.index(max(pa_walk))}")
print(f"The Minimum Distance reached was: {min(pa_walk)}, in trial: {pa_walk.index(min(pa_walk))}")
这里是评论推荐后的完整代码(我只改了最后5行)
import random
from math import sqrt
from math import hypot
import statistics
random.seed(20190101)
def takeOnePaStep():
direction = random.randint(0,3)
if direction == 0:
return (0,1)
elif direction == 1:
return (1,0)
elif direction == 2:
return (0,-1)
elif direction == 3:
return (-1,0)
def randomWalkPa(steps):
pa = [0,0]
for _ in range (steps):
nextStep = takeOnePaStep()
pa[0] += nextStep[0]
pa[1] += nextStep[1]
pasDistance = hypot(pa[0],pa[1])
return pasDistance
# paMean = statistic.mean(distance)
steps = int(input("Please enter the number of steps: "))
tries = int(input("How many times should I perform the experiment? "))
pa_walk = []
for _ in range(tries):
pa_walk.append(randomWalkPa(steps))
print(f"The Maximum Distance reached was: {max(pa_walk)}, in trial: {pa_walk.index(max(pa_walk))}")
print(f"The Minimum Distance reached was: {min(pa_walk)}, in trial: {pa_walk.index(min(pa_walk))}")
编辑:
需要注意的一件小事,在 python 中,习惯使用下划线而不是驼峰式。这意味着函数 randomWalkPa()
最好称为 random_walk_pa()
。这不是使代码正常工作所必需的,完全取决于您