有没有一种方法可以在文本文件中相互切换多行?

时间:2019-10-28 14:36:55

标签: python python-3.x text ranking leaderboard

我需要一种方法/技术来根据得分的高低对球员的文本文件进行排序。因此,如果玩家2的得分高于玩家1,则玩家2需要上移到玩家1在文本文件中的位置,反之亦然。这可能吗?

因此,我找到了在文档的每一行上列出不同类别的方式,以便于读取:

玩家1、3、2、1
玩家2、1、2、3

但是我的问题是每个播放器都有4行代码需要切换。我的文本文件如下所示:

player1
3( *这很重要
2
1

player2
1个 *
2
3

  • 因此,假设播放器2播放得更多,并且得分高于播放器1,则文本文档应该看起来像

player2
4( *
3
2

player1
3( *这是重要的行
2
1

  • 因此需要澄清;我需要对文档中得分最高的播放器首先写入文本文件的文件进行排序。这样一来,我可以打印出相关的结果排行榜

  • 请注意:我并不是在索要代码,而是一个可以用来解决此类问题的方法/一般示例(或在何处找到一个示例)。 BC我真的很茫然,在我的书和这里看。因此,如果您对此有一个答案,或者知道有相同问题的已回答主题,请对其进行评论,这将对我有很大帮助!

我知道我没有任何代码可提供,但这是不存在的。我真的只需要一些帮助就可以从哪里开始/指导我找到方法或示例

预先感谢:D

1 个答案:

答案 0 :(得分:0)

首先,您不能轻松地直接修改此类文件。方法是:读取数据,修改数据,写回数据。

我假设您可以将数据读入数据结构。例如,在您的情况下,数据可以自然地存储为4元组的列表。每个4元组看起来像这样:

Bundler could not find compatible versions for gem "actionpack":
  In Gemfile:
    cells (~> 3.11, >= 3.11.3) was resolved to 3.11.3, which depends on
      actionpack (>= 3.0)

    rails (= 4.0.12) was resolved to 4.0.12, which depends on
      actionpack (= 4.0.12)

    ransack (~> 1.5, >= 1.5.1) was resolved to 1.8.10, which depends on
      actionpack (>= 3.0, < 5.2)

    simple_form (~> 3.1) was resolved to 3.5.1, which depends on
      actionpack (> 4, < 5.2)

Bundler could not find compatible versions for gem "activerecord":
  In Gemfile:
    rails (= 4.0.12) was resolved to 4.0.12, which depends on
      activerecord (= 4.0.12)

    ransack (~> 1.5, >= 1.5.1) was resolved to 1.8.10, which depends on
      activerecord (>= 3.0, < 5.2)

Bundler could not find compatible versions for gem "activesupport":
  In Gemfile:
    carrierwave was resolved to 1.3.1, which depends on
      activesupport (>= 4.0.0)

    rails (= 4.0.12) was resolved to 4.0.12, which depends on
      activesupport (= 4.0.12)

    ransack (~> 1.5, >= 1.5.1) was resolved to 1.8.10, which depends on
      activesupport (>= 3.0, < 5.2)

    rspec-rails was resolved to 3.9.0, which depends on
      activesupport (>= 3.0)

Bundler could not find compatible versions for gem "capistrano":
  In Gemfile:
    capistrano

    capistrano-ext was resolved to 1.2.1, which depends on
      capistrano (>= 1.0.0)

    rvm-capistrano was resolved to 1.5.6, which depends on
      capistrano (~> 2.15.4)

Bundler could not find compatible versions for gem "kaminari":
  In Gemfile:
    kaminari

    kaminari-bootstrap was resolved to 3.0.1, which depends on
      kaminari (>= 0.13.0)

Bundler could not find compatible versions for gem "nokogiri":
  In Gemfile:
    nokogiri (= 1.5.11)

    capybara was resolved to 2.18.0, which depends on
      nokogiri (>= 1.3.3)

    fog (~> 1.29.0) was resolved to 1.29.0, which depends on
      nokogiri (~> 1.5, >= 1.5.11)

Bundler could not find compatible versions for gem "rails":
  In Gemfile:
    rails (= 4.0.12)

    kaminari-bootstrap was resolved to 3.0.1, which depends on
      rails

    nazca (~> 0.2.0) was resolved to 0.2.0, which depends on
      rails (>= 3.0)

    rails_autolink was resolved to 1.1.6, which depends on
      rails (> 3.1)

    spork-rails (~> 4.0) was resolved to 4.0.0, which depends on
      rails (>= 3.0.0, < 5)

Bundler could not find compatible versions for gem "railties":
  In Gemfile:
    rails (= 4.0.12) was resolved to 4.0.12, which depends on
      railties (= 4.0.12)

    sass-rails (~> 3.2.3) was resolved to 3.2.3, which depends on
      railties (~> 3.2.0.beta)

Bundler could not find compatible versions for gem "ruby":
  In Gemfile:
    ruby (~> 2.2.10.0)

    capybara was resolved to 3.15.1, which depends on
      ruby (>= 2.3.0)

(您应确保已存储分数的数字,而不是字符串,因为以后需要进行比较。)

假设您已将它们存储在名为player_list的列表中

接下来,您需要对列表进行排序。您需要的排序键是每个元组中的第二个条目,以Python的术语来说,它具有索引1(因为编号从0开始)

    ("player1", 3, 2, 1)

然后,您可以使用此排序键传递给排序功能(请参阅此https://docs.python.org/3/library/stdtypes.html#list.sort),以对列表进行就地排序:

def sort_key ( tup ): return tup[1]

(我们需要“反向”以确保顺序是递减的,而不是递增的。)

最后,您需要通过以新的顺序再次写出player_list来覆盖文件:

player_list . sort ( key = sort_key, reverse = True )

元组注释:

Tuples与列表很像,只是一旦创建了一个元组就无法更改(“不变”)。要创建元组,请使用括号()而不是方括号[]。

for (player, s1, s2, s3) in player_list:
    write_entry_to_file ( player, s1, s2, s3 ) # use whatever format you need here.

您可以用相同的方式为其编制索引

mylist = [2, 4, 6, 8]
mytuple = (2, 4, 6, 8)

并以相同的方式附加它们

mylist[0] # returns 2
mytuple[0] # returns 2 too
mylist[1:3] # returns the list [4, 6]
mytuple[1:3] # returns the tuple (4, 6)

空元组为(),具有单个元素x的元组为(x,)

在两者之间进行转换很容易:

[2,4] + [6,8] # returns [2,4,6,8]
(2,4) + (6,8) # returns (2,4,6,8)