#我想调用二进制搜索10次(使函数运行10次)。我应该这样做,然后根据结果做一个图表,我知道该怎么做。我只是不能让函数运行10次来获取图形的信息。
import matplotlib.pyplot as plt
import random
import operator
import sys
def binary_search(rodun, gildi):
lo, hi = 0, len(rodun) - 1
while lo <= hi:
mid = (lo + hi) // 2
if rodun[mid][1] < gildi:
lo = mid + 1
elif gildi < rodun[mid][1]:
hi = mid - 1
else:
return mid
def graph(x_as, y_as):
try:
plt.close()
fig = plt.figure(figsize=(8,4))
x_as = fig.add_subplot(111)
x_as.bar(x_as, y_as)
plt.x_aslim(1,11)
plt.y_aslim(-1,11)
plt.grid()
plt.show()
pass
except:
print("Úps!", sys.exc_info()[0], "occurred.")
return
def main():
x_as=[]
y_as=[]
y1= {'the':3460,'and':3192,'to':2384, 'of':1797, 'that': 1389,'was':1353,'a': 1253,
'he': 1157,'in':995,'his':944}
sorted_by_value_y1 = sorted(y1.items(), key=operator.itemgetter(1))
graph(x_as, y_as)
if __name__== "__main__":
main()
else:
print("Er inporrterað. Ekki þetta main fall")
发布于 2022-11-07 22:22:43
若要运行某些特定次数的代码,请在范围上使用for-循环:
for _ in range(10):
binary_search(params)
https://stackoverflow.com/questions/74353547
复制相似问题