Matching numbers in strings in regex and converting into integers
本问题已经有最佳答案,请猛点这里访问。
我尝试使用re.findall()匹配给定文本体中的所有数字,并将其转换为整数。我知道像
以下是我的资料:
1 2 3 4 5 6 7 8 9 10 11 | import re regex_list = [] sample ="Here are a bunch of numbers 7746 and 12 and 1929 and 8827 and 7 and 8837 and 128 now convert them" for line in sample: line = line.strip() if re.findall('([0-9]+)', line): regex_list.append(int(line)) print(regex_list) |
输出:
1 | [7, 7, 4, 6, 1, 2, 1, 9, 2, 9, 8, 8, 2, 7, 7, 8, 8, 3, 7, 1, 2, 8] |
号
期望输出:
1 | [7746, 12, 1929, 8827, 7, 8837, 128] |
您的问题是,当前正在逐字符循环,此时您可以将regex应用于整行。
1 2 3 4 | >>> import re >>> s ="Here are a bunch of numbers 7746 and 12 and 1929 and 8827 and 7 and 8837 and 128 now convert them" >>> [int(j) for j in re.findall(r'[0-9]+', s)] [7746, 12, 1929, 8827, 7, 8837, 128] |
看看@chrisz的答案,找到更好的解决方案。
但是,如果你想知道你的错在哪里:
使用
1 2 3 4 5 6 7 8 9 10 11 | regex_list = [] sample ="Here are a bunch of numbers 7746 and 12 and 1929 and 8827 and 7 and 8837 and 128 now convert them" for line in sample.split(): line = line.strip() if re.findall('([0-9]+)', line): regex_list.append(int(line)) print(regex_list) # [7746, 12, 1929, 8827, 7, 8837, 128] |
号
但是,由于您是单独获得单词,因此不需要使用regex。您可以直接向我们介绍
1 2 3 4 | for line in sample.split(): line = line.strip() if line.isdigit(): regex_list.append(int(line)) |
或者,简单地使用列表理解:
1 2 3 | num_list = [int(word) for word in sample.split() if word.isdigit()] print(num_list) # [7746, 12, 1929, 8827, 7, 8837, 128] |
。