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)
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:
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 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'
This question already has answers here:
Remove substring only at the end of string [duplicate]
(11 answers)
Closed 4 years ago.
I want to keep the file names without the.csv extension, but using rstrip('.csv') deletes the last letter in the strings ending in s:
data_files = [
"ap_2010.csv",
"class_size.csv",
"demographics.csv",
"graduation.csv",
"hs_directory.csv",
"sat_results.csv"
]
data_names = [name.rstrip('.csv') for name in data_files]
I get this results:
["ap_2010", "class_size", "demographic","graduation","hs_directory", "sat_result"]
The end s of strings demographics and sat_results has been removed, why does this happen??
This is because rstrip() strips all characters separately from the end of your string.
>>> 'abcdxyx'.rstrip('yx')
'abcd'
This will search for y and x to strip from the right side of your string. If you like to remove the .csv you can use split instead.
>>> "ap_2010.csv".split('.')[0]
"ap_2010"
Also for Filenames it is good practice to use the function os.path.splitext:
>>> import os
>>> os.path.splitext('ap_2010.csv')[0]
"ap_2010"
You can get your intended output with this:
data_files = [
"ap_2010.csv",
"class_size.csv",
"demographics.csv",
"graduation.csv",
"hs_directory.csv",
"sat_results.csv"
]
data_names = [name.replace('.csv','') for name in data_files]
This question already has answers here:
Regular expression to filter list of strings matching a pattern
(5 answers)
Closed 3 years ago.
I think this is a easy task but I'm new to regex so can't figure it out. I want to filter a list that contains something like this: "ANY"-"ANY"-"ANY"
Input:
List1 = ["AB.22-01-01", "AB.33-01-44", "--4", "AA.44--05", "--"]
Output:
List2 = ["AB.22-01-01", "AB.33-01-44"]
Each item will contain two "-" but I only want to get the ones with text on each sides of the "-".
Try this using re module :
import re
p = re.compile('^.+-.+-.+$')
l1 = ["AB.22-01-01", "AB.33-01-44", "--4", "AA.44--05", "--"]
l2 = [ s for s in l1 if p.match(s) ]
You can use a regular expressions. It will return all element that don't contains --
>>> import re
>>> pat = re.compile(r'^((?!--).)*$')
>>> [i for i in List1 if pat.match(i)]
['AB.22-01-01', 'AB.33-01-44']
Demo