该计划的原则是根据足球运动员的表现(进球数)得出最终排名。因此,我必须根据目标的数量来归纳分数。
仅有3名足球运动员,根据目标数量排名从最小到最大。 有两轮更多的最终排名。我必须学会操纵数组和函数(不使用函数zip,sort等等)
这是我的数组
name = [" Henry"," Owen"," Drogba"] =>足球运动员的名字
goal1 = [18,12,6] =>它是关于每个球员在第一轮中的进球数量
goal2 = [6,8,5] =>这是关于每个球员在第二轮中的进球数量
增益= [2,4,6] =>根据目标数得分属性如果你标记了大部分目标,你将获得6分。所以最大。
我不知道它是否清楚?对不起我的英语不好。
我的问题,我有另一个数组点= [0,0,0]我怎样才能在这个数组中获得最终排名的总积分?对于每一轮我都希望用当前点加上新的点。
我希望用新功能预览最终排名。 非常感谢你。
def printOne(name, goal, title, current_pts):
print("\t\t\t\t{}\n".format(title))
for i in range(len(name)):
print("\t\t\t\t {} \t {} points {} goals".format(name[i], gain[i] ,goal[i]))
print("=======================================================")
return current_pts
def sort(name, goal1, goal2, pts):
swap = True
while swap:
swap = False
for i in range(len(name) - 1):
if goal1[i + 1] < goal1[i]:
name[i], name[i + 1] = name[i + 1], name[i]
goal1[i], goal1[i + 1] = goal1[i + 1], goal1[i]
goal2[i], goal2[i + 1] = goal2[i + 1], goal2[i]
pts[i], pts[i + 1] = pts[i + 1], pts[i]
swap = True
return name, goal1, goal2, pts
def test(name, goal, pts):
for i in range(len(name)):
goalTotal.append(goal1[i] + goal2[i])
print(name[i] + " \t ???? points \t " + str(goalTotal[i]) + " goals : ")
return name, goal, pts
name = ["Henry", "Owen", "Drogba"]
point = [0,0,0]
goal1 = [18, 12, 6]
goal2 = [6, 8, 5]
gain = [2,4,6]
goalTotal = []
name, goal1, goal2, point = sort(name, goal1, goal2, point)
point = printOne(name, goal1, "First round", point)
name, goal2, goal1, point = sort(name, goal2, goal1, point)
point = printOne(name, goal2, "Second round", point)
test(name, goalTotal, "Ranking total ")
结果如下
First round
Drogba 2 points 6 buts
Owen 4 points 8 buts
Henry 6 points 10 buts
=======================================================
Second round
Henry 2 points 4 buts
Drogba 4 points 5 buts
Owen 6 points 6 buts
=======================================================
Final Ranking
Drogba 6 points
Henry 8 points
Owen 10 points
答案 0 :(得分:0)
我不确定我是否理解正确,但你的意思是这样吗?
import numpy as np
def printOne(name,gain, goal, title, current_pts):
print("\t\t\t\t{}\n".format(title))
for i in range(len(name)):
print("\t\t\t\t {} \t {} points {} buts \t {} total points".format(name[i], gain[i] ,goal[i],current_pts[i]))
print("=======================================================")
return current_pts
def trier( goal, pts, gain):
minIndex = goal.index(min(goal))
maxIndex = goal.index(max(goal))
middleIndex = 0
if minIndex + maxIndex == 2:
middleIndex = 1
elif minIndex + maxIndex == 1:
middleIndex = 2
gain_per_person = [0,0,0]
gain_per_person[maxIndex] = gain[2]
gain_per_person[middleIndex] = gain[1]
gain_per_person[minIndex] = gain[0]
pts = np.add(pts, gain_per_person)
return gain_per_person, pts
name = ["Henry", "Owen", "Drogba"]
point = [0,0,0]
goal1 = [18, 12, 6]
goal2 = [6, 8, 5]
gain = [2,4,6]
gain1, point = trier(goal1, point, gain)
point = printOne(name, gain1, goal1, "First round", point)
gain2, point = trier(goal2, point, gain)
point = printOne(name, gain2, goal2, "Second round", point)
结果如下:
First round
Henry 6 points 18 buts 6 total points
Owen 4 points 12 buts 4 total points
Drogba 2 points 6 buts 2 total points
=======================================================
Second round
Henry 4 points 6 buts 10 total points
Owen 6 points 8 buts 10 total points
Drogba 2 points 5 buts 4 total points
=======================================================
这是你在找什么?
编辑在评论中回答问题
&#34;感谢您的帮助。在编写最终结果时,我编辑了我的第一篇文章。你如何在第二轮中添加第二轮的积分?最终排名? &#34;
在开始点= [0,0,0]
然后为goal1调用trier方法
trier([18,12,6], [0,0,0], [2,4,6])
goal1 = [6,4,2]
的gain_per_person
在pts = np.add(pts, gain_per_person)
行,[0,0,0]
点和[6,4,2]
被添加到彼此,这导致pts为[6,4,2]
然后将其作为第二个返回值返回并保存在point
。
然后为goal2
调用trier方法
trier([6,8,5], [6,4,2], [2,4,6])
goal1 = [4,6,2]
的gain_per_person
在pts = np.add(pts, gain_per_person)
行,[4,6,2]
点和[6,4,2]
被添加到彼此,这导致pts为[10,10,4]
然后将其作为第二个返回值返回并保存在point
。
编辑2
如果名称是["Henry", "Owen", "Drogba"]
而goal1的结果是[6,4,2]
而目标2的结果是[2,6,4]
那么期望的结果是[8,10,6]
将这个故事放在python代码中
points_goal1 = [6, 4, 2]
points_goal2 = [2, 6, 4]
points_total = [0, 0, 0]
for index in range(3):
print(index)
print("points_goal1[", index, "] =", points_goal1[index])
print("points_goal2[", index, "] =", points_goal2[index])
print()
points_total[index] = points_goal1[index] + points_goal2[index]
print("=======================================================")
print("points_goal1 =", points_goal1)
print("points_goal2 =", points_goal2)
print("points_total =", points_total)
结果是
0
points_goal1[ 0 ] = 6
points_goal2[ 0 ] = 2
1
points_goal1[ 1 ] = 4
points_goal2[ 1 ] = 6
2
points_goal1[ 2 ] = 2
points_goal2[ 2 ] = 4
=======================================================
points_goal1 = [6, 4, 2]
points_goal2 = [2, 6, 4]
points_total = [8, 10, 6]