I've run some tests here and I think I've found the answer.
My var self.name has a <class 'PyQt4.QtCore.QString'> type, since I'm getting it from a QtWidget.
Someone, please, correct me if I'm wrong but from the tests I run and from the docs (http://doc.qt.io/qt-5/qstring.html#replace) it seems that the replace method in <class 'PyQt4.QtCore.QString'> happens inplace. Whereas for the Python str, it does not since python strings are immutable.
So, in short:
- Python
str:inplace=False(Python strings are immutable) PyQt4.QtCore.QString:inplace=True
Anyway, hope this can be helpful
Answer from Eduardo on Stack OverflowI've run some tests here and I think I've found the answer.
My var self.name has a <class 'PyQt4.QtCore.QString'> type, since I'm getting it from a QtWidget.
Someone, please, correct me if I'm wrong but from the tests I run and from the docs (http://doc.qt.io/qt-5/qstring.html#replace) it seems that the replace method in <class 'PyQt4.QtCore.QString'> happens inplace. Whereas for the Python str, it does not since python strings are immutable.
So, in short:
- Python
str:inplace=False(Python strings are immutable) PyQt4.QtCore.QString:inplace=True
Anyway, hope this can be helpful
When you do self.name you are actually replacing in place on the objects name string.
However when you do str(self.name) you are replacing on the new object created by str which is not self.name. Hence self.name remains unchanged.
'in-place' string modifications in Python - Stack Overflow
Using replace() method
python - why pandas.replace inplace = True doesnt work - Stack Overflow
python - Why pandas DataFrame replace method does not work (inplace=True argument is used) - Stack Overflow
You're iterating through the elements within the DataFrame, in which case I'm assuming it's type str (or being coerced to str when you replace). str.replace doesn't have an argument for inplace=....
You should be doing this instead:
dataset['ver'] = dataset['ver'].str.replace('.', '')
Sander van den Oord in the comments is quite correct to point out:
dataset['ver'].replace("[.]","", inplace=True, regex=True)
This is the way we do operations on a column in Pandas because in general, Pandas tries to optimize over for loops. The Pandas developers consider for loops the among least desirable pattern for row-wise operations in Python (see here.)
Don't use a string, use something mutable like bytearray:
#!/usr/bin/python
s = bytearray("my dog has fleas")
for n in xrange(len(s)):
s[n] = chr(s[n]).upper()
print s
Results in:
MY DOG HAS FLEAS
Edit:
Since this is a bytearray, you aren't (necessarily) working with characters. You're working with bytes. So this works too:
s = bytearray("\x81\x82\x83")
for n in xrange(len(s)):
s[n] = s[n] + 1
print repr(s)
gives:
bytearray(b'\x82\x83\x84')
If you want to modify characters in a Unicode string, you'd maybe want to work with memoryview, though that doesn't support Unicode directly.
The Python analog of your C:
for(int i = 0; i < strlen(s); i++)
{
s[i] = F(s[i]);
}
would be:
s = "".join(F(c) for c in s)
which is also very expressive. It says exactly what is happening, but in a functional style rather than a procedural style.
Hi guys, I had a quick question about the exercise I was working on.
For using the replace() method and printing the result, we have to assign it to another variable and print that new variable.
sentence = sentence.replace(‘hey’, ‘hi’) print(sentence)
But for something such as the sort method we don’t need to assign it to a new variable and just use it straight up.
list = […] list.sort() print(list)
Why is it that some methods you need to assign it to a new variable, while others work while not assigning it to a new variable and the original is changed. Thank you.
Sorry about format, I’m on mobile.
Use fileinput.FileInput, with inplace=True. printed line will be used as a replacement string for each line.
myfile = fileinput.FileInput("inputRegex.txt", inplace=True)
for line in myfile:
line = re.sub(r"[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?",
"foundValue",
line.rstrip())
print(line)
UPDATE
re.sub can accept a function as replacement. It will be called with match object and the return value of the function is used as a replacement string.
The following is slightly modified version to use captured groups (to use in replacement function).
line = re.sub(r"([+-]? *)(\d+(?:\.\d*)?|\.\d+)([eE][+-]?\d+)?",
lambda m: m.group(1) + re.sub('(\..{4}).*', r'\1', m.group(2)) + (m.group(3) or ''),
line.rstrip())
import fileinput
import re
myfile = open("inputRegex.txt", "r")
def changePrecision(matchObj):
return str(round(float(matchObj.group(0).replace(" ","")),4))
for line in myfile:
newLine = re.sub(r"[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?", changePrecision, line)
print newLine
I hope this is what you are looking for