python - 在for循环中划分数组?
问题描述
我的老师给了我一项任务,其中一个问题要我将数组中的所有内容除以26.22
(全程马拉松)。我整天都在做这个工作,完全被卡住了,有人可以告诉我如何做这个工作吗?
这就是我到目前为止所拥有的
import string
forename = []
surname = []
distance = []
farthest_walk = []
marathon = []
#Opening the text file and sorting variables
data = open("members.txt","r")
for line in data:
value = line.split(',')
forename.append(value[0])
surname.append(value[1])
distance.append(value[2])
#Closing the text file
data.close()
Results = open("Results.txt","w+")
Results.write("The number of whole marathons walked be each member is:\n")
for count in range(len(distance)):
if float(distance[count])/ 26.22 = temp:
marathon.append
Results.write(forename[count]+":")
Results.write(surname[count]+":")
Results.write(marathon[count])
Results.close()
它应该以 , 结尾,Forename
但我不明白它是如何到达那里的。Surname
WholeMarathosRun
解决方案
你几乎到了那里。对于每个名字,你需要计算他跑了多少马拉松,这可以通过以下操作来实现:
temp = float(distance[count])/ 26.22
这不需要在if
声明中。
然后你需要在输出文件中写下这个值在名字后面:
Results.write(forename[count]+":")
Results.write(surname[count]+":")
Results.write(temp)
# line break such that each result stay in one line
Results.write("\n")
所有这些行都在for
您已经拥有的最后一个循环中。
推荐阅读
- typescript - 更改角度材料中的选择时如何从后端获取url到iframe
- javascript - 如何制作复选框过滤器?
- javascript - 如果语句为真,脚本在块内返回 console.log() 但不返回
- asp.net-core - 登录管理器
.PasswordSignInAsync() 抛出 System.Text.Json 对象循环异常 - clojure - 在 .edn 文件中使用 #inst 将时间戳播种到 clojure 中的 jdbc 数据库
- python - 使用 Pandas 进行事件搜索
- java - java中这两个数组语句的区别
- android - Google 控制台上的 java.lang.RuntimeException
- nativescript - Nativescript RadSideDrawer。我不需要返回按钮
- bash - 将 unix 命令 1 输出到 A 列,将 unix 命令 2 输出到 B 列到相同的 .csv 文件