Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 3.x 通过对字符串进行迭代,如何提取更多索引和相应的值?_Python 3.x_String - Fatal编程技术网

Python 3.x 通过对字符串进行迭代,如何提取更多索引和相应的值?

Python 3.x 通过对字符串进行迭代,如何提取更多索引和相应的值?,python-3.x,string,Python 3.x,String,给定我的字符串,我试图只提取数值,但是使用索引和查找函数,我只能提取值7.22。 如何拾取符号“=”后的其他三个数值 作为输出,我得到了7.22打印的字符串长度的倍!一、 另一方面,您需要打印出7.22、3.12、30.1和1.78 import re print(re.findall("\d+\.\d+", "[(Mark=7.22, Paola=3.12) , (Mark=30.1, Paola=1.78)]")) 输出: ['7.22', '3.

给定我的字符串,我试图只提取数值,但是使用索引和查找函数,我只能提取值7.22。 如何拾取符号“=”后的其他三个数值

作为输出,我得到了7.22打印的字符串长度的倍!一、 另一方面,您需要打印出7.22、3.12、30.1和1.78

import re

print(re.findall("\d+\.\d+", "[(Mark=7.22, Paola=3.12) , (Mark=30.1, Paola=1.78)]"))
输出:

['7.22', '3.12', '30.1', '1.78']
[7.22, 3.12, 30.1, 1.78]
如果需要转换为float

print(list(map(float, re.findall("\d+\.\d+", "[(Mark=7.22, Paola=3.12) , (Mark=30.1, Paola=1.78)]"))))
输出:

['7.22', '3.12', '30.1', '1.78']
[7.22, 3.12, 30.1, 1.78]
如果要将结果保存到变量中

x = list(map(float, re.findall("\d+\.\d+", "[(Mark=7.22, Paola=3.12) , (Mark=30.1, Paola=1.78)]")))

非常感谢这个程序,但是如果我想把这个向量保存在一个变量x中(例如),我应该怎么做呢?再次感谢你的帮助@luna8899更新了我的答案。如果答案解决了你的问题,就接受它problem@luna8899结果值是一个列表,因此为了获得前2个元素-
first_element=lst[0];第二个元素=lst[1]
您可以使用列表切片@luna8899
第一个元素=lst[:2]
请阅读-总结是,这不是解决志愿者问题的理想方式,可能会对获得答案产生反作用。请不要将此添加到您的问题中。