Python的新特性。为什么findall不搜索到行尾并匹配所有数字组

Python的新特性。为什么findall不搜索到行尾并匹配所有数字组,python,python-2.7,Python,Python 2.7,一个简单的重新匹配一行中的数字的方法是: /[0-9]+/gm Will match (------> Match) t1est ------->1 23 ------->23 foo bar 304958 ------->304958 bar as 4497 our 6702 personal 8454 assistants who can take care of many things -----> 4497 6702 8454 当我尝试使

一个简单的重新匹配一行中的数字的方法是:

/[0-9]+/gm
Will match (------> Match)
t1est ------->1
23    ------->23
foo
bar
304958    ------->304958
bar
as 4497 our 6702 personal 8454 assistants who can take care of many things    -----> 4497 6702 8454
当我尝试使用python进行同样的操作时,我的程序只匹配第一次出现的一组数字,似乎在匹配之后不会匹配到该行的其余部分,因此只获得每行的第一个匹配。我需要每行的所有匹配项(所有数字组)。 这是我的密码:

import re
hand = open('sum1.txt')
numlist = list()
for line in hand:
        line = line.rstrip()
        stuff = re.findall('[0-9]+',line)
        if len(stuff) == 0  : continue
        numbers = int(stuff[0])
        numlist.append(numbers)
print 'Max ',max(numlist)
print 'Min ',min(numlist)
print 'Sum ',sum(numlist)
print numlist
从文件读取中提取:

"...
Conversing with Python

1052 Now that we have a word and a simple sentence that we know in Python, 8135
we need to know how to start a conversation with Python to test 
our new language skills.

Before 5561 you 517 can 1218 converse with Python, you must first install the Python
 software on your computer and learn how to start Python on your 
computer.  That is too much detail for this chapter so I suggest
that you consult www.pythonlearn.com where I have detailed
instructions and screencasts of setting up and starting Python 
on Macintosh and Windows systems.  At some point, you will be in 
a terminal or command window and you will type python and 
8877 the Python interpreter will start executing in interactive mode
and appear somewhat as follows:
interactive mode
..."
查看运行后的代码,它将仅匹配第一组数字。请参阅输出:

$ python myfirstdatapython.py
Max  9703
Min  5
Sum  244547
[3036, 4497, 7449, 3665, 7114, 956, 8003, 6662, 6482, 9634, 8805, 9703, 2834, 7221, 2981, 5415, 6872, 4806, 5460, 3538, 9663, 8752, 4509, 1004, 4034, 3482, 1052, 5561, 8877, 8062, 279, 2054, 918, 8687, 7073, 2923, 8824, 1079, 5, 2572, 5616, 9552, 829, 6015, 9548, 8402, 42]
分析: 见上图55618877,它从5561跳到517和1218 我对阵列的推送有什么问题? 或者我应该改进正则表达式。 注意:必须使用re


谢谢

跳过它们是因为您告诉Python跳过它们:

    numbers = int(stuff[0])
    numlist.append(numbers)

stuff
是您分析的行的所有匹配数字的数组,但您只提取第一个(
[0]
)并保存它,这实际上会忽略/删除该行上的所有其他后续数字。

它们被跳过,因为您告诉Python跳过它们:

    numbers = int(stuff[0])
    numlist.append(numbers)

stuff
是分析行的所有匹配数字的数组,但您只能提取第一个(
[0]
)并保存它,这实际上会忽略/删除该行上的所有其他后续数字。

未经测试,但这里有一个解决方案和正在发生的事情

import re

# don't use `list()` here - use `[]` an empty list
numlist = []
# We can use a with statement here so the file automatically
# closes on an error or when the block ends...
with open('sum1.txt') as fin:
    # iterate line by line
    for line in fin:
        # Get all the numbers as strings
        nums = re.findall('\d+'), line)
        # Convert to actual integers
        nums = [int(n) for n in nums]
        # Add them to the end of our list
        numlist.extend(nums)

# now do some work with `numbers`
# ...

未经测试,但这里有一个解决方案和正在发生的事情

import re

# don't use `list()` here - use `[]` an empty list
numlist = []
# We can use a with statement here so the file automatically
# closes on an error or when the block ends...
with open('sum1.txt') as fin:
    # iterate line by line
    for line in fin:
        # Get all the numbers as strings
        nums = re.findall('\d+'), line)
        # Convert to actual integers
        nums = [int(n) for n in nums]
        # Add them to the end of our list
        numlist.extend(nums)

# now do some work with `numbers`
# ...

“因为你故意只取第一个数字,而忽略了
numbers=int(stuff[0])
?@Jon谢谢!如果超过一个提示-
re.findall
已经返回一个包含所有匹配项的
列表,将查看如何添加lopp。。。从那里开始:)'因为你故意只取第一个数字,而忽略了
numbers=int(stuff[0])
?@Jon谢谢!如果超过一个提示-
re.findall
已经返回一个包含所有匹配项的
列表,将查看如何添加lopp。。。从那里开始:)@Mark谢谢!当我第一次使用stack时,我想我的问题得到了回答。@Mark谢谢!当我第一次使用stack时,我想我的问题已经得到了回答。哇,我刚刚开始使用Python,这让我大吃一惊。感谢Jon花时间评论代码并提供解决方案。哇,我刚刚开始使用Python,这让我大吃一惊。感谢Jon花时间评论代码并提供解决方案。