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']
Related
This question already has answers here:
What is the best way to remove accents (normalize) in a Python unicode string?
(13 answers)
Closed 7 months ago.
I am removing accents and special characters from a DataFrame but the way I am doing it does not seem optimal to me, how can I improve it?
Thanks.
Code:
import pandas as pd
m = pd.read_excel('file.xlsx')
print(m)
m['hola']=m['hola'].str.replace(r"\W","")
m['hola']=m['hola'].str.replace(r"á","a")
m['hola']=m['hola'].str.replace(r"é","e")
m['hola']=m['hola'].str.replace(r"í","i")
m['hola']=m['hola'].str.replace(r"ó","o")
m['hola']=m['hola'].str.replace(r"ú","u")
m['hola']=m['hola'].str.replace(r"Á","A")
m['hola']=m['hola'].str.replace(r"É","E")
m['hola']=m['hola'].str.replace(r"Í","I")
m['hola']=m['hola'].str.replace(r"Ó","O")
m['hola']=m['hola'].str.replace(r"Ú","U")
print(m)
You could make a dictionary with the special characters as the keys and their replacements as the values:
d = {}
d["á"] = "a".... etc.
x = "árwwwe"
for character in x:
if character in d.keys():
x = x.replace(character, d[character])
print(x)
Output:
arwwwe
This question already has answers here:
Keeping only certain characters in a string using Python?
(3 answers)
Closed 10 months ago.
How would I remove everything but certain characters from a string such as (+,1,2,3,4,5,6,7,8,9,0)
math = ("tesfsgfs9r543+54")
output = ("9543+54")
You can use regular expressions.
import re
output = re.sub("[^+0-9]", "", math)
Using iterators is also possible, but it probably is slower. (not recommended)
output = ''.join(ch for ch in math if ch in "+1234567890")
Using a for loop.
def keep_characters(string, char_collection):
result = ""
for ch in string:
if ch in char_collection:
result += ch
return result
output = keep_characters(math, "+1234567890")
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:
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:
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