3

我在执行此类操作时遇到问题,假设我们有一个字符串

teststring = "This is a test of number, number: 525, number: 585, number2: 559"

我想将 525 和 585 存储到一个列表中,我该怎么做?

我以一种非常愚蠢的方式做到了,有效,但必须有更好的方法

teststring = teststring.split()
found = False
    for word in teststring:
        if found:
            templist.append(word)
            found = False
        if word is "number:":
            found = True

有正则表达式的解决方案吗?

追问:如果我要存储525、585和559怎么办?

4

5 回答 5

5

使用re模块:

>>> re.findall(r'number\d*: (\d+)',teststring)
['525', '585', '559']

\d是任何数字 [0-9]
*表示从 0 到无限次
()表示要捕获的内容
+表示从 1 到无限次

如果您需要将生成的字符串转换为ints,请使用map

>>> map(int, ['525', '585', '559'])
[525, 585, 559]

或者

列表理解

>>> [int(s) for s in ['525', '585', '559']]
[525, 585, 559]
于 2013-08-01T22:33:29.393 回答
4

您可以使用正则表达式组来完成此操作。这是一些示例代码:

import re
teststring = "This is a test of number, number: 525, number: 585, number2: 559"
groups = re.findall(r"number2?: (\d{3})", teststring)

groups然后包含数字。此语法使用正则表达式组。

于 2013-08-01T22:30:53.037 回答
3

你可以试试这个:

import re
[int(x) for x in re.findall(r' \d+', teststring)]

这会给你:

[525, 585, 559]
于 2013-08-01T22:31:06.050 回答
1

我建议:

teststring = "This is a test of number, number: 525, number: 585, number2: 559"
# The following does: "This is a test of number, number: 525, number: 585, number2: 559" -> ["525, number", "585, number2", "559"]
a = teststring.split(': ')[1:]
# The following does: ["525, number", "585, number2", "559"] -> ["525", " number", "585", " number2", "559"]
b = [i.split(',') for i in a]
# The following does: [["525", " number"], ["585", " number2"], ["559"]] -> ["525", "585", "559"]
c = [i[0] for i in b]
>>> c
['525', '585', '559']
于 2013-08-01T22:38:46.133 回答
0

它不是世界上最有效的代码,但它仍然可能比正则表达式更好:

tokens = teststring.split()
numlist = [val for key, val in zip(tokens, tokens[1:]) if key == 'number:']

对于您的后续和更一般的查询:

def find_next_tokens(teststring, test):
    tokens = teststring.split()
    return [val for key, val in zip(tokens, tokens[1:]) if test(key)]

可以称为:

find_next_tokens(teststring, lambda s: s.startswith('number') and s.endswith(':'))

如果要搜索的键来自用户输入,这将有所帮助:

find_next_tokens(teststring, lambda s: s in valid_keys)
于 2013-08-01T22:30:48.647 回答