This question already has answers here:
How to detect a floating point number using a regular expression
(7 answers)
python: extract float from a python list of string( AUD 31.99)
(5 answers)
Closed 4 years ago.
i want to extract the numbers from the following string:
FRESENIUS44.42 BAYER64.90 FRESENIUS MEDICAL CARE59.12 COVESTRO45.34 BASF63.19
I've tried the following approach but that didn't work:
l = []
for t in xs.split():
try:
l.append(float(t))
except ValueError:
pass
The result should be 44.42 64.90 59.12 45.34 63.19
Thank you!
import re
list = ["FRESENIUS44.42", "BAYER64.90" "FRESENIUS MEDICAL CARE59.12", "COVESTRO45.34", "BASF63.19",]
newList = [float(re.findall("\d+\.\d+", i)[0]) for i in list]
print(newList)
First, we extract the floats using regex, then we convert into floats and append to list using list comprehension.
import re
myStr = 'FRESENIUS44.42 BAYER64.90 FRESENIUS MEDICAL CARE59.12 COVESTRO45.34 BASF63.19'
outList = re.findall(r"[-+]?\d*\.\d+|\d+", myStr)
['44.42', '64.90', '59.12', '45.34', '63.19']
finalStr = ' '.join(outList)
'44.42 64.90 59.12 45.34 63.19'
Related
This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 2 years ago.
How can I get a string after and before a specific substring?
For example, I want to get the strings before and after : in
my_string="str1:str2"
(which in this case it is: str1 and str2).
Depending on your use case you may want different things, but this might work best for you:
lst = my_string.split(":")
Then, lst will be: ['str1', 'str2']
You can also find the index of the substring by doing something like:
substring = ":"
index = my_string.find(":")
Then split the string on that index:
first_string = my_string[:index]
second_string = my_string[index+len(substring):]
This question already has answers here:
Python Remove Comma In Dollar Amount
(4 answers)
Closed 2 years ago.
I have a list of currency in string. I want to extract only the decimal numbers from that list.
how can I do so ?
I tried the below code but it gives me an extra point in front of each value.
list1 = ['Rs.35,916.00', 'Rs.35,916.00', 'Rs.45,000.00']
for i in list1:
value = (sub(r'[^\d.]', '', i))
print(value)
Output:
.35916.00
.35916.00
.45000.00
expected output:
35916.00
35916.00
45000.00
list1 = ['Rs.35,916.00', 'Rs.35,916.00', 'Rs.45,000.00']
for i in list1:
value = (sub(r'Rs\.|[^\d.]', '', i))
print(value)
This question already has answers here:
Split binary number into groups of zeros and ones
(3 answers)
How to split a binary string into groups that containt only ones or zeros with Java regular expressions? [duplicate]
(5 answers)
Closed 2 years ago.
I need to split ones and zeros in any binary representation like this.
code = 10001100
output_list = [1,000,11,00]
I couldnt find the pattern.
and I am using python3.x
You don't really need a regex for this problem. You can use groupby from itertools to do this:
import itertools
code = "10001100"
gs = [list(g) for _, g in itertools.groupby(code)]
If you want to use regex, then:
import re
code = r'10001100'
output_list = re.findall(r'(0+|1+)', code)
regex is not required. Here is pythonic way to do it:
code = '10001100'
output_list = []
interim_list = [code[i] + ',' if i != len(code)-1 and code[i] != code[i+1] else code[i] for i in range(len(code))]
output_list.append(''.join(interim_list))
print(output_list)
>>> print(output_list)
['1,000,11,00']
This question already has answers here:
How to use regex with optional characters in python?
(5 answers)
Closed 4 years ago.
I am trying to filter data in an array with the help of a pattern \d\.\d. The elements in the array might sometimes consist of strings as well. I try to use the re.findall function to get a list of the decimal numbers within my strings in the array but my code doesn't recognise all decimal numbers.
My code is as below -
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)
The output is only [2.7] whereas I need to also get [.3]. What change is required in my code
You can use:
z = re.findall(r'\d?\.\d',a)
This question already has answers here:
Extracting only characters from a string in Python
(7 answers)
How do you filter a string to only contain letters?
(6 answers)
Closed 6 years ago.
I was trying to figure out how to list just the letters in a string and ignore the numbers or any other characters. I figured out how to do it using the for loop, but I couldn't find out how to do it without using the for loop.
This is how I used the for loop:
>>> a = "Today is April 1, 2016"
for i in a:
if i.isalpha():
list(i)
Any help will be appreciated!
You can use filter for this:
>>> ''.join(filter(str.isalpha, a))
'TodayisApril'
list(set([x for x in a if x.isalpha()]))
this should do it :)