如何将此特定字符串更改为特定输入

时间:2011-04-10 21:32:43

标签: python string sorting integer

我的python程序中有以下行

print "Player 1: " +str(player1points)

在我的程序中计算'player1points'。

结果产量:

Player 1: 3

其中'3'是该程序为该次运行计算的内容。

然而,对于一个特定的功能,我设法从这里获得帮助

def sort_players(players):
r"""Sort the players by points.

>>> print sort_players('Player 1: 3\n'
...                    '\n'
...                    'Player 2: 4\n'
...                    '\n'
...                    'Player 3: 3\n'
...                    '\n'
...                    'Player 4: 5\n')
Player 4: 5
Player 2: 4
Player 1: 3
Player 3: 3
"""
# split into a list
players = players.split("\n")

# filter out empty lines
players = [player for player in players if player != '']

def points(player_report):
    """Parse the number of points won by a player from a player report.

    A "player report" is a string like 'Player 2: 6'.
    """
    import re
    # Match the last string of digits in the passed report
    points = re.search(r'\d+$', player_report).group()
    return int(points)

# Pass `points` as a "key function".
# The list will be sorted based on the values it returns.
players.sort(key=points, reverse=True)

# Make the sorted list back into a string.
return "\n".join(players)

该功能只接受输入:

Player 1: 3

而不是:

"Player 1: " +str(player1points)

即使在我看来他们都产生相同的结果,我怎么能够转换

"Player 1: " +str(player1points)

进入相应的输入,以便函数接受它。

示例:

sort_players('Player 1: 3\n'
             '\n'
             'Player 2: 4\n'
             '\n'
             'Player 3: 3\n')

会给我

Player 2: 4
Player 1: 3
Player 3: 3

示例2:

sort_payers('Player 1: +str(player1points)\n'
            '\n'
            'Player 2: +str(player2points)\n'
            '\n'
            'Player 3: +str(player3points)\n')

会给我

AttributeError:'NoneType' object has no attribute 'group'

3 个答案:

答案 0 :(得分:3)

'Player 1: +str(player1points)\n'

只是一个字符串,即使它包含可能是Python代码的东西。值得庆幸的是,Python不会神奇地评估它。该字符串将字面传递给该函数。查看上面一行的语法高亮,并将其与此进行比较,实际上将player1points添加到字符串Player 1:

'Player 1: '+str(player1points)+'\n'

你知道,必须将str调用和+放在字符串文字之外,这样它们对Python意味着什么。 (当你在它时,你可能想要切换到字符串格式,即"Player 1: %d" % player1points"Player 1: {}".format(player1points)。)

错误消息具体由此行引起:

points = re.search(r'\d+$', player_report).group()

由于该行中没有任何数字(仅"+str(player1points)"),re.search将返回None而不是匹配对象 - None显然不知道关于团体的任何事情。

答案 1 :(得分:0)

您通常应该使用formatting strings而不是+运算符来进行python中的连接。我也不确定你为什么要将已经有点值的字符串传递给提取点值的函数?

你可以尝试这样的事情(虽然我不确定你为什么会失败......)

mystr = "Player 1: %d" % player1points
print points(mystr)

答案 2 :(得分:0)

  

排序(iterable [,cmp [,key [,reverse]]])

     

返回一个新的排序列表   来自可迭代的项目。

您的代码是一个划水池。但似乎你要处理的数据是一个字符串。所以我这样做:

ch = '''\
Player 1: 3

Player 2: 4

Player 3: 3

Player 4: 5
'''
print ch

import re

pat = re.compile('(Player (\d+): (\d+))')

print '------------'
print '\n'.join(x[2] for x in
                sorted((-int(points),number,report)
                       for (report, number, points)
                       in pat.findall(ch)))

结果

Player 1: 3

Player 2: 4

Player 3: 3

Player 4: 5

------------
Player 4: 5
Player 2: 4
Player 1: 3
Player 3: 3

如果输入数据不是字符串,请解释它是什么,我将修改代码。