我不明白你的代碼試圖做什麼data
與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提供了一個快捷方式:在.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()
內給出一個名字;它可以在一條線上構建和返回。
你應該在'print_results'中循環yoru玩家嗎?它只顯示你的列表中的4個索引 – dm03514
行「data_list.append(效率)」到「a.reverse()」的縮進都有點偏離。請記住,Python對縮進很敏感。這是你的代碼粘貼錯誤嗎? – dupersuper