Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取使用模式找到的数据列表_Python_Python 3.x - Fatal编程技术网

Python 如何获取使用模式找到的数据列表

Python 如何获取使用模式找到的数据列表,python,python-3.x,Python,Python 3.x,我试图借助模式\d\.\d过滤数组中的数据。数组中的元素有时也可能由字符串组成。我尝试使用re.findall函数获取数组中字符串中的十进制数列表,但我的代码无法识别所有十进制数 我的代码如下- import re import itertools str1 = "2.7" str2 = ".3" str3 = "." str4 = "2" str5 = "sushruth" x = [str1,str2,str3,str4,str5] y = [] for a in x: z =

我试图借助模式
\d\.\d
过滤数组中的数据。数组中的元素有时也可能由字符串组成。我尝试使用
re.findall
函数获取数组中字符串中的十进制数列表,但我的代码无法识别所有十进制数

我的代码如下-

import re 
import itertools
str1 = "2.7"
str2 = ".3"
str3 = "."
str4 = "2"
str5 = "sushruth" 
x = [str1,str2,str3,str4,str5]
y = []
for a in x:
    z = re.findall(r'\d\.\d',a)
    if z:
        print(z)
输出仅为
[2.7]
,而我还需要获得
[.3]
。“我的代码”中需要进行哪些更改您可以使用:

z = re.findall(r'\d?\.\d',a)

谢谢我得到的输出看起来有点像
[2.7][.3]
有没有办法获得列表或数组中所有检测到的模式,比如
[2.7,3]
?@Sushruth
[e代表e,如果re.match(r'\d?\d',e)]