python-3.x - 我无法理解代码。谁能帮我吗?
问题描述
marks = {}
for _ in range(int(input())):
line = input().split()
marks[line[0]] = list(map(float, line[1:]))
print('%.2f' %(sum(marks[input()])/3))
我是 python 新手。你能告诉我这段代码的含义吗?
我无法理解它。
解决方案
这段代码的作用:
# initialized a dictionary type names marks
marks = {}
# The input() method will pause and wait for someone to input data in the command line
# The range() method will create an array of int given the a number
# example: range(5) will create [0, 1, 2, 3, 4]
# In this case it will take the string returned from input() convert it to an integer
# and use that as the value.
# The for loop will, run as many times as there are elements "in" the array created
# the _ is just a silly variable name the developer used because
# he is not using the value in the array anywhere.
for _ in range(int(input())):
# Get a new input from the user
# split the string (it uses spaces to cut the string into an array)
# example if you type "one two three" it will create ["one", "two", "three"]
# store the array in the variable line
line = input().split()
# add/replace the element using the first string in the line as key
# line[0] is the first element in the array
# lint[1:] is the array containing all the elements starting at index 1 (the second element)
# map() is a function that will call the function float on each elements of the array given. basically building an array with the values [float(line[1]), float(line[2])…]
# list will convert the array into a list.
marks[line[0]] = list(map(float, line[1:]))
# this last line asks the user for one more value
# gets the list in the marks dictionary using the value inputed by the user
# calculates the sum of all the floats in that list.
# divides it by 3 and prints the results as a floating point number with 2 decimal places.
print('%.2f' %(sum(marks[input()])/3))
推荐阅读
- r - FuzzywuzzyR 错误尝试应用非函数
- kubernetes - Helm 使用本地存储安装 elk-stack - 节点亲和性冲突
- java - Swing 中平滑的 Snake 运动
- python-3.x - 类型错误:fit() 缺少 1 个必需的位置参数:'X'
- android - 更改工具栏溢出菜单列表的高度
- rabbitmq - 概念死信交换在我的环境中不起作用
- postgresql - (Postgresql) make table trigger function insert更改行的ID值到日志表中,用于自定义复制
- apache-kafka - 我有一个 0 领导和 0 isr 的主题:UNKNOWN_TOPIC_OR_PARTITION
- active-directory - 当用户从 ADFS SSO 登录时,当用户权限在 Active Directory 中更改时注销用户
- c# - Azure 功能 - 在 Azure 存储中找不到方法