我不明白你的代码试图用data
vs.做什么data_list
。我重写它以摆脱data
. 此外,在您计算后将efficiency
其附加到列表中,但随后您似乎再次将其从列表中拉出score
。我只是摆脱了那个。
对于处理文件,最佳实践是使用with
语句,所以我重写了它。
此外,您一次将字符串项转换为一个整数,而您可以通过列表理解一次完成所有这些项。我希望列表推导对您来说不是问题,因为它们使代码更清晰。第一个列表推导.strip()
在三个字符串中的每一个上调用方法函数来获取名称。第二个以一种方便的方式转换所有整数。
我没有创建一个名为的临时列表a
然后反转该列表,而是reverse=True
在sorted()
. 现在列表以相反的顺序构建,正是您想要的。
正如其他人所指出的,您的打印功能需要一个循环来打印列表中的内容。由于打印函数中的消息说它打印前 50 个,我将列表切片更改为在打印函数内部进行。现在 print 函数接受一个可选参数,指定要打印的项目数;它的默认值为 50,因此如果您不指定其他值,它将打印前 50 个项目。
虽然您不需要这样做,但 Python 有一个常见的功能,即放在if __name__ == __main__:
代码之前。你可以在这里看到一个解释:What does if __name__ == "__main__": do?
def get_data_list (file_name):
player_list=[]
with open(file_name, "r") as f:
for line in f:
# split line on commas, and convert items to integer values
# make a list of the integer values.
items = line.split(',')
first_name = items[2].strip()
last_name = items[3].strip()
team_name = items[4].strip()
data_list = [int(x) for x in items[6:]
gp = data_list[0]
mins = data_list[1]
pts = data_list[2]
oreb = data_list[3]
dreb = data_list[4]
reb = data_list[5]
asts = data_list[6]
stl = data_list[7]
blk = data_list[8]
to = data_list[9]
pf = data_list[10]
fga = data_list[11]
fgm = data_list[12]
fta = data_list[13]
ftm = data_list[14]
tpa = data_list[15]
tpm = data_list[16]
efficiency = ((pts+reb+asts+stl+blk)-(fgm-ftm-to))/gp
player_tuple = efficiency, last_name, first_name, team_name
player_list.append(player_tuple)
return sorted(player_list, reverse=True)
def print_results(lst, how_many=50):
"""Print the result in a nice format"""
template = '{:<20}{:<20s}, {:<15s}{:<5s}'
print("The top {} players based on efficiency are: ".format(how_many))
print('*'*75)
for tup in lst[:how_many]:
print(template.format(tup[0], tup[1], tup[2], tup[3]))
if __name__ == "__main__":
file_name1 = input("File name: ")
result_list = get_data_list(file_name1)
print_results(result_list)
现在我要进一步平滑它。这是在 Python 中使用更高级的特性,但它们是使事情变得更方便的特性,而不是只是棘手的事情。
首先,我们将使用生成器表达式并将项目直接解包为变量名,而不是使用列表推导式构建列表,然后按索引号选择项目。生成器表达式就像一个列表推导式,只是它不是构建一个列表,而是提供一个可以循环的“迭代器”,或者可以解压缩为变量名,如我在这里展示的那样。
其次,在打印函数中,我们只想按顺序打印元组中的所有值。Python 提供了一个快捷方式:*
在调用内部的元组前面放置一个 a.format()
表示“解包并使用解包后的值作为此函数调用的参数”。
def get_data_list (file_name):
player_list=[]
with open(file_name, "r") as f:
for line in f:
# Split line on commas and convert each item to integer. Unpack
# values directly into variable names. We are using a
# generator expression to convert all the items to integer,
# and Python's ability to unpack an iterator into a tuple.
items = line.strip().split(',')
# use list slicing to select just the three string values
first_name, last_name, team_name = (s.strip() for s in items[2:5])
# Use a generator expression to convert all values to int.
# Unpack directly to variable names using tuple unpacking.
# Put parentheses so Python won't worry about multiple lines
# of variable names.
(
gp, mins, pts, oreb, dreb, reb, asts,
stl, blk, to, pf, fga, fgm, fta, ftm,
tpa, tpm
) = (int(x) for x in items[6:])
efficiency = ((pts+reb+asts+stl+blk)-(fgm-ftm-to))/gp
player_tuple = efficiency, last_name, first_name, team_name
player_list.append(player_tuple)
return sorted(player_list, reverse=True)
def print_results(lst, how_many=50):
"""Print the result in a nice format"""
template = "{:<20}{:<20s}, {:<15s}{:<5s}"
print("The top {} players based on efficiency are: ".format(how_many))
print('*'*75)
for player_tuple in lst[:how_many]:
print(template.format(*player_tuple))
if __name__ == "__main__":
file_name1 = input("File name: ")
result_list = get_data_list(file_name1)
print_results(result_list)
编辑:这是另一个经过编辑的版本。这个将解析一行的逻辑分解为一个player_tuple
自己的函数。这get_data_list()
很短。
def player_tuple(line):
# Split line on commas and convert each item to integer. Unpack
# values directly into variable names. We are using a
# generator expression to convert all the items to integer,
# and Python's ability to unpack an iterator into a tuple.
items = line.strip().split(',')
# use list slicing to select just the three string values
first_name, last_name, team_name = (s.strip() for s in items[2:5])
# use a generator expression to convert all values to int
# unpack directly to variable names using tuple unpacking
(
gp, mins, pts, oreb, dreb, reb, asts,
stl, blk, to, pf, fga, fgm, fta, ftm,
tpa, tpm
) = (int(x) for x in items[6:])
efficiency = ((pts+reb+asts+stl+blk)-(fgm-ftm-to))/gp
return efficiency, last_name, first_name, team_name
def get_data_list(file_name):
with open(file_name, "r") as f:
player_list = [player_tuple(line) for line in f]
return sorted(player_list, reverse=True)
def print_results(lst, how_many=50):
"""Print the result in a nice format"""
template = "{:<20}{:<20s}, {:<15s}{:<5s}"
print("The top {} players based on efficiency are: ".format(how_many))
print('*'*75)
for player_tuple in lst[:how_many]:
print(template.format(*player_tuple))
if __name__ == "__main__":
file_name1 = input("File name: ")
result_list = get_data_list(file_name1)
print_results(result_list)
现在我们有了一个函数,我们可以进一步player_tuple()
简化。get_data_list()
我不会重复整个程序,只是简化的get_data_list()
. 如果我必须解决这个问题,这可能是我要编写的代码。
def get_data_list(file_name):
with open(file_name, "r") as f:
return sorted((player_tuple(line) for line in f), reverse=True)
在这里,我们甚至没有明确地构建列表。我们只需创建一个提供所有player_tuple
值的生成器表达式,然后直接将其传递给sorted()
. 没有必要在里面给这个列表命名get_data_list()
;它可以在一行中构建和返回。