2

Given my string, I'm trying to extract only the numeric values, but with the index and find functions I can only extract the value 7.22. How do I pick up the other three numeric values after the symbol "=" ?

stringa="[(Mark=7.22, Paola=3.12) , (Mark=30.1, Paola=1.78)]"
for i in stringa:
    i=stringa.index("=")
    n=stringa[i+1:i+5]
    print(n)

As output I get 7.22 printed the number of times as long as the string length! I, on the other hand, need you to print out 7.22, 3.12, 30.1 and 1.78.

halfer
  • 19,824
  • 17
  • 99
  • 186
luna8899
  • 55
  • 5
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Aug 05 '20 at 15:55

1 Answers1

4
import re

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

Output:

['7.22', '3.12', '30.1', '1.78']

If you need to convert to float

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

Output:

[7.22, 3.12, 30.1, 1.78]

If you want to save the result to a variable

x = list(map(float, re.findall("\d+\.\d+", "[(Mark=7.22, Paola=3.12) , (Mark=30.1, Paola=1.78)]")))
bigbounty
  • 16,526
  • 5
  • 37
  • 65
  • thank you very much the program works, but if I wanted to save this vector within a variable x (for example) how should I do it? thanks again for the help!!! – luna8899 Aug 05 '20 at 13:36
  • 1
    @luna8899 updated my answer. Accept the answer if it has solved your problem – bigbounty Aug 05 '20 at 13:37
  • Excuse me, my question is this: once I get the vector x with the numbers how do I extract only the first two values? For example [7.22, 3.12] – luna8899 Aug 06 '20 at 09:55
  • 1
    @luna8899 the resulting value is a list, so in order to get the first 2 elements - `first_element = lst[0]; second_element = lst[1]` – bigbounty Aug 06 '20 at 09:57
  • Ah ok excuse me, what if I want to take more values and put them inside a vector? – luna8899 Aug 06 '20 at 10:00
  • 1
    You can use list slicing https://stackoverflow.com/questions/509211/understanding-slice-notation – bigbounty Aug 06 '20 at 10:02
  • I apologize again, I promise this is the last question, but once I have the vector x,y=[7.22, 3.12, 30.1, 1.78]. How can I automatically and simultaneously take the first two values each time and put them inside an operation? – luna8899 Aug 06 '20 at 10:14
  • 1
    @luna8899 `first_two_values = lst[:2]` – bigbounty Aug 06 '20 at 10:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219334/discussion-between-luna8899-and-bigbounty). – luna8899 Aug 06 '20 at 12:30
  • I'm sorry, but I still have a problem on the program, I have to take individually, within a for cycle, the values of the list so that then at each cycle I put this value within a function, how should I do it? list=[7.22, 3.12, 30.1, 1.78] for i in list: take the first value Put this value in function z – luna8899 Aug 06 '20 at 13:44