![](/img/trans.png)
[英]Remove all values below certain threshold and shift columns up in Pandas
[英]Remove bands below certain threshold in file
numberofbands = int(input("How many bands are there in the competition? "))
print("Input each band’s name pressing enter after each one")
file = open("scores.txt","w")
for loop in range(numberofbands):
name = input("\nEnter the name of the band: ")
votes = input("Enter how many votes that band received: ")
file.write(name + "," + votes + "," + "\n")
file.close()
number_of_lines = len(open("scores.txt").readlines( ))
def removebelowthreshold():
threshold = int(input("Choose the threshold of scores to remove bands which are below"))
removebelowthreshold()
該代碼將獲取樂隊的名稱和樂譜並將其寫入文件。
如果他們的分數低於用戶給出的某個閾值,它應該刪除文件中樂隊的詳細信息。 希望你能幫忙。
先感謝您
為什么要先編寫文件,然后才嘗試過濾它? 文件 I/O 操作在您的邏輯中是更昂貴的操作,因此您應該盡量減少它們。 您應該在其他輸入之前詢問閾值,然后在文件寫入循環中添加一個條件:
threshold = int(input("Choose the threshold of scores to remove bands which are below"))
numberofbands = int(input("How many bands are there in the competition? "))
print("Input each band’s name pressing enter after each one")
file = open("scores.txt","w")
for loop in range(numberofbands):
name = input("\nEnter the name of the band: ")
votes = input("Enter how many votes that band received: ")
if int(votes) >= threshold:
file.write(name + "," + votes + ",\n")
file.close()
number_of_lines = len(open("scores.txt").readlines( ))
顯然,您需要進行多種數據類型和范圍檢查才能使程序健壯,但基本邏輯是這樣的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.