This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 3 months ago.
So, I've seen a lot of answers for removing duplicate characters in strings, but I'm not trying to remove all duplicates - just the ones that are beside each other.
This is probably a lot more simple than what I'm doing, but this is what I've been attempting to do (and failing miserably at)
for j in range(2, len(string)-1):
char = string[j]
plus = string[j+1]
minus = string[j-1]
if char == plus or char == minus:
string.replace(char, "")
For reference, the code SHOULD act as:
input: ppmpvvpmmp
output: pmpvmp
But instead, the output does not change at all.
Again, I'm aware that this is most likely very easy and I'm overcomplicating, but I'm genuinely struggling here and have tried a lot of similar variations
I would use a regular expression replacement here:
inp = "ppmpvvpmmp"
output = re.sub(r'(\w)\1', r'\1', inp)
print(output) # pmpvpmp
The above assumes that a duplicate is limited to a single pair of same letters. If instead you want to reduce 3 or more, then use:
inp = "ppmpvvvvvpmmmp"
output = re.sub(r'(\w)\1+', r'\1', inp)
print(output) # pmpvpmp
This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 3 years ago.
So, I have a string "AB256+74POL". I want to extract the numbers only into a list say num = [256,74]. How to do this in python?
I have tried string.split('+') and followed by iterating over the two parts and adding the characters which satisfy isdigit(). But is there an easier way to that?
import re
a = 'AB256+74POL'
array = re.findall(r'[0-9]+', a)
"".join([c if c.isdigit() else " " for c in mystring]).split()
Explanation
Strings are iterable in python. So we iterate on each character in the string, and replace non digits with spaces, then split the result to get all sequences of digits in a list.
This question already has answers here:
re.findall behaves weird
(3 answers)
Closed 4 years ago.
Given a string, I want to find all the substrings consisting of two or three '4,'.
For example, given '1,4,3,2,1,1,4,4,3,2,1,4,4,3,2,1,4,4,4,3,2,'
I want to get ['4,4,', '4,4,', '4,4,4'].
str_ = '1,4,4,3,2,1,1,4,4,3,2,1,4,4,3,2,1,4,4,3,2,'
m = re.findall(r"(4,){2,3}", str_)
what I get is :
['4,', '4,', '4,', '4,']
what's wrong?
It seems to me that the parenthesis wrapping '4,' is interpreted as grouping but not telling Python '4' and ',' should occur together. However, I don't know how to do this.
Just use non-capturing group (online version of this regex here):
import re
s = '1,4,3,2,1,1,4,4,3,2,1,4,4,3,2,1,4,4,4,3,2,'
print(re.findall(r'(?:4,?){2,3}', s))
Prints:
['4,4,', '4,4,', '4,4,4,']
EDIT:
Edited regex to capture 2 or 3 elements "4,"
This question already has answers here:
Remove characters from beginning and end or only end of line
(5 answers)
Closed 4 years ago.
So, I have the following string "........my.python.string" and I want to remove all the "." until it gets to the first alphanumeric character, is there a way to achieve this other than converting the string to a list and work it from there?
You can use re.sub:
import re
s = "........my.python.string"
new_s = re.sub('^\.+', '', s)
print(new_s)
Output:
my.python.string
This question already has answers here:
replace string in pandas dataframe
(3 answers)
Closed 6 years ago.
I want to remove the brackets and contents withing the brackets from a string.
I tried following code:
a['Street Name'].str.replace('\(.*)','')
But it is not working. Can anybody please tell me what is wrong with this statement?
Try this:
import re
s = "I want to remove all words in brackets( like (this) and ((this)) and ((even) this))."
while True:
s_new = re.sub(r'\([^\(]*?\)', r'', s)
if s_new == s:
break
s = s_new
print(s_new) # I want to remove all words in brackets.