有效地更改列表列表中的值

时间:2014-01-22 23:18:07

标签: python python-3.x

我有一个如下所示的列表:

[['B Borg', '3', '3', '1.0'], ['F Perry', '7', '8', '0.875'], ['R Nadal', '3', '5', '0.6']]

我希望用户选择其中两个网球运动员进行模拟网球比赛,随机选择获胜者。因此,如果我选择B Borg和F Perry相遇,B Borg会随机获胜。最终结果应如下所示:

[['B Borg', '4', '4', '1.0'], ['F Perry', '7', '9', '0.875'], ['R Nadal', '3', '5', '0.6']]

列表中的第一个元素是名称,第二个是赢得的比赛,第三个是总比赛,第四个是赢率。

有没有'整洁'的方法呢?我尝试过使用多个if-elif-else块。确定它有效,但它有很多文字。

2 个答案:

答案 0 :(得分:3)

使用课程!!

class TennisPlayer(object):
    def __init__(self,name,wins=0,games_played=0,win_pct=None):
        self.name = name
        self.wins = wins
        self.games_played = games_played

        # Note that although I do take win_pct as a parameter, I never use
        # it anywhere. This trick may be useful for you if you end up maintaining
        # code someone else has written and don't need part of their data
        # structure anymore, but don't feel like refactoring code is worth the time

    @property
    def win_pct(self):
        return self.wins/self.games_played

    # as per lanzz, the @property token basically makes this a function that can
    # be called like an instance variable.
    #
    # >>> player = TennisPlayer("Foo Bar",2,3) # 2 wins 1 loss
    # >>> player.win_pct
    # 0.6666666666666666
    #
    # You could probably make this prettier using string formatting.
    # Something like return "{:.2f}%".format(self.wins/self.games_played*100)
    # but it will make data manipulation much harder. Consider writing
    # a TennisPlayer.print_stats() function that will just pretty print
    # all the stats to stdout

    def win_game(self,other):
        self.games_played += 1
        self.wins += 1
        other.games_played +=1

lst = [['B Borg', '3', '3', '1.0'], ['F Perry', '7', '8', '0.875'], ['R Nadal', '3', '5', '0.6']]

tennisplayers = [TennisPlayer(*player) for player in lst]

# randomly choose B Borg (tennisplayers[0]) and F Perry (tennisplayers[1])

tennisplayers[0].win_game(tennisplayers[1])

您可以想象的实现的数量与此类似。最好的(在我看来,无论如何)也是如此实现TennisMatch类:

class TennisMatch(object):
    def __init__(self,player1,player2):
        if type(player1) == list:
            # implement special handlers for doubles games
        else:
            self.player1 = player1
            self.player2 = player2

    def play_match(self):
        # do some stuff here
        # to determine who wins and
        # who goes crying to mommy
        for player in [player1,player2]:
            if player == winner: player.wins += 1
            player.games_played += 1

然后你可以忽略我放在def win_game类中的TennisPlayer,因为它实际上只是一个执行不佳的setter。这样做效果更好。

答案 1 :(得分:2)

另一个有类的解决方案(与adsmith基本相同,但我已经输入了,所以哦。)

import random

class Player:
    def __init__(self, name):
        self.name = name
        self.wins = 0
        self.games = 0
    def win(self):
        self.wins += 1
        self.games += 1
    def lose(self):
        self.games += 1
    def __str__(self):
        win_pct = self.wins/self.games
        return '{:10} {:10} {:10} {:10.2}'.format(self.name, self.wins, self.games, win_pct)

def play_game(p1, p2):
    winner, loser = random.sample([p1, p2], 2)
    winner.win()
    loser.lose()

players = [Player(name) for name in ['B Borg', 'F Perry', 'R Nadal']]

for _ in range(20):
    play_game(*random.sample(players, 2))
for player in players:
    print(player)

示例结果:

B Borg              7         15       0.47
F Perry             7         14        0.5
R Nadal             6         11       0.55