replace

How to update strings in a list based on condition

故事扮演 提交于 2020-05-14 09:09:17
问题 I am stuck in a situation where I have a list and I want to change subnet /21 to /24 . x = ['192.168.8.1/21', '192.168.4.36/24', '192.168.47.54/16'] for a in x: if "21" in (a[-2:]): print(a) else: print("carry on") Now it's printing right values but how I can change the values of a[-2:] 21 to 24 I fail to understand. Output: 192.168.8.1/21 carry on carry on 回答1: You cannot change a part of a string as strings are immutable. But you can replace the string with a corrected version. x = ['192

Converting NaN in dataframe to zero

≡放荡痞女 提交于 2020-05-11 07:28:21
问题 I have dictionary and created Pandas using cars = pd.DataFrame.from_dict(cars_dict, orient='index') and sorted the index (columns in alphabetical order cars = cars.sort_index(axis=1) After sorting I noticed the DataFrame has NaN and I wasn't sure if the really np.nan values? print(cars.isnull().any()) and all column shows false. I have tried different method to convert those "NaN" values to zero which is what I want to do but non of them is working. I have tried replace and fillna methods and

Converting NaN in dataframe to zero

两盒软妹~` 提交于 2020-05-11 07:27:11
问题 I have dictionary and created Pandas using cars = pd.DataFrame.from_dict(cars_dict, orient='index') and sorted the index (columns in alphabetical order cars = cars.sort_index(axis=1) After sorting I noticed the DataFrame has NaN and I wasn't sure if the really np.nan values? print(cars.isnull().any()) and all column shows false. I have tried different method to convert those "NaN" values to zero which is what I want to do but non of them is working. I have tried replace and fillna methods and

By using replace and regex: I have captured c, but I want to set it at the end of osonant plus “ay” with replace in Javascript

我的梦境 提交于 2020-04-30 06:49:12
问题 Here is he link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin. By using replace and regex: I have captured c , but I want to set it at the end of onsonant plus ay using the replace function in JavaScript Here is my code: function translatePigLatin(str) { let regEx=/([bcd-fgh-klmn-pqrst-vwxyz])/i console.log(str.replace(regEx, '$1,')) } translatePigLatin("consonant"); 回答1: See if that's what you want function

Atom Editor: RegEx replace to uppercase/lowercase

折月煮酒 提交于 2020-04-29 07:15:52
问题 I'm trying to replace some characters in a string with their uppercase/lowercase equivalents in Atom Editor. Let’s say I have the string: some:test:sequence and want the result: Some:Test:Sequence I’m aware of things like \u$1 and \l$1 , but they do not work in Atom, as Atom is using JS-style RegEx. The JS-RegEx solutions I found, however, always involve calling a function (see example here), which is not possible in Atom, afaik. Does anyone know if there is a way to achieve this? I also don

How to replace every second occurence of a word in a text file

邮差的信 提交于 2020-04-15 20:56:26
问题 In a file called sample.txt , I have the following text: Once there is a tortoise and a rabbit. The rabbit was fast, tortoise was slow. Rabbit used to mock the tortoise. Once the rabbit challenged the tortoise for a race. Tortoise accepted rabbit’s request. Rabbit was overconfident. Rabbit thought to win the race. Rabbit ran fast. Then rabbit got tired. Rabbit wanted to take rest. So rabbit slept under the tree. Tortoise kept going and won the race. How to replace every second occurrence of

How to use different separators (/ , |) in a regular expression

自闭症网瘾萝莉.ら 提交于 2020-04-15 08:14:12
问题 Modifying a Perl script , i got this: $var =~ s,/$,,; it seems to be a regex pattern, but i was expecting to found "/" (or "|") instead of "," as separator. So the question is: when and why one should use in a regex pattern "/" or "|" or ","? 回答1: In Perl, in the substitution operator, as well as many other operators, you can substitute the delimiter for almost any punctuation character, such as s#/$## s=/$== s!/$!! Which one to use when is a matter of what you need at the time. Preferably

How to use different separators (/ , |) in a regular expression

倾然丶 夕夏残阳落幕 提交于 2020-04-15 08:08:04
问题 Modifying a Perl script , i got this: $var =~ s,/$,,; it seems to be a regex pattern, but i was expecting to found "/" (or "|") instead of "," as separator. So the question is: when and why one should use in a regex pattern "/" or "|" or ","? 回答1: In Perl, in the substitution operator, as well as many other operators, you can substitute the delimiter for almost any punctuation character, such as s#/$## s=/$== s!/$!! Which one to use when is a matter of what you need at the time. Preferably

Python: Replacing multiple specific words from a list with re.sub

送分小仙女□ 提交于 2020-04-13 08:02:48
问题 I have the following string and list 'changewords'. I would like to replace the '{word from list} \n' with '{word from list}:' I don't want to replace all instances of '\n'. string = "Foo \n value of something \n Bar \n Another value \n" changewords = ["Foo", "Bar"] Desired Output: 'Foo: value of something \n Bar: Another value \n' I have tried the following for i in changewords: tem = re.sub(f'{i} \n', f'{i}:', string) tem Output: 'Foo \n value of something \n Bar: Another value \n' and

Python: Replacing multiple specific words from a list with re.sub

南楼画角 提交于 2020-04-13 08:02:33
问题 I have the following string and list 'changewords'. I would like to replace the '{word from list} \n' with '{word from list}:' I don't want to replace all instances of '\n'. string = "Foo \n value of something \n Bar \n Another value \n" changewords = ["Foo", "Bar"] Desired Output: 'Foo: value of something \n Bar: Another value \n' I have tried the following for i in changewords: tem = re.sub(f'{i} \n', f'{i}:', string) tem Output: 'Foo \n value of something \n Bar: Another value \n' and