Python 2.7:从列表中弹出(Python 2.7: Popping from a list)

编程入门 行业动态 更新时间:2024-10-24 23:25:34
Python 2.7:从列表中弹出(Python 2.7: Popping from a list)

我正在制作像太空入侵者这样的游戏。 游戏将从游戏中获取所有分数,对其进行排序,然后将分数保存到csv文件中。 我这样做的方法是将分数保存到未排序的csv文件,读取和排序这些值,然后将排序的值保存到不同的排序的csv文件。 但是,我希望我的csv文件只存储前10个分数。 这是我目前用于此过程的代码:

if col: with open("rec_Scores.csv", "ab") as f: #adding new score into unsorted file w = csv.writer(f, delimiter = ",") w.writerow([curr_score]) with open ("rec_Scores.csv", "rb") as csvfile: #reads file, converts values to integers and sorts file (including the new score) r = csv.reader(csvfile) scores = list(r) scores_int = [int(score[0]) for score in scores] bubbleSort(scores_int) scores_int.pop() print(scores_int) with open("srt_Scores.csv", "wb") as sortfile: #Should write a new csv file every time containing the sorted scores w = csv.writer(sortfile, delimiter = ",") w.writerows([[v] for v in scores_int])

我知道这不是执行此过程的最有效方法,但它主要起作用。 如果有人可以帮助我让程序只存储前10个分数,那将非常感激。

Ps:这是我第一次使用Python中的csv文件,所以我知道它是混乱和低效的,但分数存储和排序很好,我只想限制值的数量。

I am currently making a game like space invaders. The game will take in all the scores from the game, sort them, and then save the scores to a csv file. My method of doing this is by saving scores to an unsorted csv file, reading and sorting those values, then saving the sorted values to different sorted csv file. However, I want my csv file to only store the top 10 scores. This is the code that I currently have for this process:

if col: with open("rec_Scores.csv", "ab") as f: #adding new score into unsorted file w = csv.writer(f, delimiter = ",") w.writerow([curr_score]) with open ("rec_Scores.csv", "rb") as csvfile: #reads file, converts values to integers and sorts file (including the new score) r = csv.reader(csvfile) scores = list(r) scores_int = [int(score[0]) for score in scores] bubbleSort(scores_int) scores_int.pop() print(scores_int) with open("srt_Scores.csv", "wb") as sortfile: #Should write a new csv file every time containing the sorted scores w = csv.writer(sortfile, delimiter = ",") w.writerows([[v] for v in scores_int])

I know this isn't the most efficient method to do this process but it works mostly. If anyone could help me make the program just store the top 10 scores it would be much appreciated.

P.s: This is my first time working with a csv file in Python so I know it is messy and inefficient, but the scores are stored and sorted fine, I just want to limit the number of values.

最满意答案

如果上面的代码对你来说是“正常工作”(你可以添加新的分数,阅读并对它们进行排序),那么要将它们限制为10,你需要做的就是切掉前10个分数。 替换此行:

scores_int.pop()

...有了这个:

scores_int = scores_int[:10]

假设您的最佳分数排在第一位。 如果没有,那么你想要最后 10个项目:

scores_int = scores_int[-10:]

在列表中有10个项目之前执行此操作是安全的。 在这种情况下,任何给定的行都不会做任何事情。

If the above code is otherwise "working" for you (you're able to add new scores, read and sort them), then to limit them to 10 all you need to do is slice off the first 10 scores. Replace this line:

scores_int.pop()

...with this:

scores_int = scores_int[:10]

That assumes your best scores come first. If not, then you want the last 10 items instead:

scores_int = scores_int[-10:]

It's safe to do this before you have 10 items in the list. In that case either of the given lines will simply do nothing.

更多推荐

本文发布于:2023-07-19 09:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1176912.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹出   列表中   Python   Popping   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!