我需要在'name‘旁边按从高到低的顺序排列'score’,但它不会这样做,它会在列表中打印'score‘和'name’,而不是按顺序
(“获胜者”的名字和“积分”的整数)
import csv
score=points
username=winner
with open ("write.csv", "a", newline='') as file:
fields=['score', 'name']
writer=csv.DictWriter(file, fieldnames=fields)
writer.writerow({'score' : score, 'name' : username})
with open ("write.csv", "r") as file:
sortlist=[]
reader=csv.reader(file)
for i in reader:
sortlist.append(i)
for i in range(len(sortlist)): #it does not order correctly
if i != 0:
time.sleep(0.1)
print()
print("The End")
time.sleep(1)
print()
print("LeaderBoards")
print()
time.sleep(1)
for i in range(len(sortlist)1):
print(sortlist[i])我希望输出结果从最高到最低排列在名称旁边。
['53', 'James']
['26', 'Bob']
['65', 'John']
To-
['65', 'John']
['53', 'James']
['26', 'Bob']
All i need is a line or a couple for it to print highest to lowest alongside the 'username'发布于 2019-11-12 05:51:18
我所需要的就是sortlist.sort(反向=真)
发布于 2019-11-10 09:13:14
使用熊猫更容易,就像这样... (在此之前,请确保您已安装了熊猫...pip安装pandas)
import pandas as pd
df=pd.read_csv('write.csv',delimiter=',')
#With pandas
print(df.sort(['score'], ascending=[0]))
#To list
list = df.values.tolist()
print(list)发布于 2019-11-10 18:49:53
在您的例子中,您希望使用两个列表进行比较,而不是使用字典。
在回答中,我添加了建设性的评论,解释了在哪里做什么。我没有包括重复的分数清理,而是评论提示,因为我怀疑这是家庭作业。然而,你会对我的代码有一种感觉,那就是如何解决过去的问题(当这篇文章发表时,这不是你问题的一部分)。指纹是用来显示发生了什么。好好享受吧。
# an extra data entry included to show duplicate issue you might encounter with your dataset.
list_1 = [['53', 'James'], ['65', 'James'], ['26', 'Bob'], ['65', 'John']]
list_temp = []
values_seen = []
values_dup = [] # add here your score duplicates (see comment below)
ids = 0 # positional ID tag for each data-entry if score is similar to one in list (gets
# filtered out if you make it a dict but remains in lists). This allows you to id
# duplicates in names and scores but are in fact two different persons.
for item in list_1: # add unique position identifier (in case there are similar names in the list with different score.)
ids +=1
list_temp.append((item[0], ids))
values_seen.append(item[0]) # hook to check later for duplicate scores.
l = list(reversed(sorted(list_temp))) # sorted score from highest to lowest
print (list_temp)
print (l)
list_final = []
# link name to its score.
for item in l:
print (item[1])
list_final.append([item[0], list_1[item[1]-1][1]])
print (list_final, '\n', values_seen)
print ("...and the winner is : %s" % list_final[0]) # procedure does not take into account shared winners.另一种选择是:
with open ("write.csv", "r") as file:
sortlist=[]
reader=csv.reader(file)
for i in reader:
sortlist.append(i)
sortlist.sort(reverse=True)
...snippet...更新:
在with open ("write.csv", "r") as file:和for i in range(len(sortlist)):之间,可以像我上面建议的那样执行排序。
with open ("write.csv", "r") as file:
sortlist=[]
reader=csv.reader(file)
for i in reader:
sortlist.append(i)
# a variation of l = list(reversed(sorted(list_temp))) using "sortlist" goes here
# sorted score from highest to lowest. Keep in mind duplicate scores.
for i in range(len(sortlist)): #it does not order correctly
if i != 0:
time.sleep(0.1)https://stackoverflow.com/questions/58784490
复制相似问题