Replace word, but another word with same letter format got replaced

2024/11/15 8:07:29

Im trying to replace a word in python, but another word with same letter format got replaced

example :

initial        : 'bg bgt'
goal           : 'bang banget'
current result : 'bang bangt'

heres what my code currently looks like:

def slangwords(kalimat):words = kalimat.split(' ')for word in words:if any(x in word for x in "bg"):kalimat = kalimat.replace("bg","bang")if any(x in word for x in "bgt"):kalimat = kalimat.replace("bgt","banget")return kalimatprint(slangwords('bg bgt'))

n ill appreciate more if u can show me how to replace these slangword more effective and efficient, thanks

Answer

That is because you replace bg before bgt (which is a bigger substring), you need to change the order.

Also, you don't need if any(x in word for x in "bg"), that checks if every letter is present in the word and not if the substring is present in the same order, plus, you don't need any verification before using str.replace, if the strin isn't there, it won't do anything

You just need

def slangwords(kalimat):return kalimat.replace("bgt", "banget").replace("bg", "bang")

Better and not order-dependent

Use a dictionnary, and replace each word with its substitute

def slangwords(kalimat):replacements = {'bg': 'bang','bgt': 'banget'}words = kalimat.split(' ')for i, word in enumerate(words):words[i] = replacements.get(word, word)return " ".join(words)
https://en.xdnf.cn/q/119072.html

Related Q&A

Python: Split timestamp by date and hour

I have a list of timestamps in the following format:1/1/2013 3:30I began to learn python some weeks ago and I have no idea how to split the date and time. Can anyone of you help me?Output should be on…

ModuleNotFoundError: when importing curses in IDE

I get the error ModuleNotFoundError: No module named _curses every time I try to uses curses in VS Code or PyCharm. But it works in the command prompt (Im on Windows BTW) Code is from Tech With Tim tut…

Add new column in a csv file and manipulate on the on records

I have 4 csv files named PV.csv, Dwel.csv, Sess.csv, and Elap.csv. I have 15 columns and arouind 2000 rows in each file. At first I would like to add a new column named Var in each file and fill up the…

Xpath returns null

I need to scrape the price of this page: https://www.asos.com/monki/monki-lisa-cropped-vest-top-with-ruched-side-in-black/prd/23590636?colourwayid=60495910&cid=2623 However it is always returning …

I am getting an Index error as list out of range. I have to scan through many lines

import nltk import random from nltk.tokenize import sent_tokenize, word_tokenizefile = open("sms.txt", "r") for line in file:#print linea=word_tokenize(line)if a[5] == SBI and a[6]=…

How can i append two classes in JQuery

I have implemented a chat box message (live chat) using django and now i want to add css, but i have problem on how to append multiple classes on messege sent. For example, i want to show other user me…

After installing PyBluez on Windows8.1 I get DLL %1 not valid win32 app

I have installed PyBluez-0.22.win32.exe on a 64bit machine with Python 2.7 (they didnt have a 64bit version). Then I get the following error: ImportError:DLL load failed:%1 is not valid Win32 applicati…

Flask Apache on AWS EC2 - Read/Write Failing

So Ive been stumped by this problem for a day now. Im relatively new to AWS EC2 so have been experimenting with Python Flask apps on it.I have an Ubuntu instance, and can get a flask app to run fine on…

AttributeError: unicode object has no attribute pop

I have this piece of python code in gae.def post(self):cases=self.request.get(cases)while cases:logging.info("cases: %s " % cases)case=cases.pop()Which produces this log.INFO 2012-09-19 2…

How to set platform when using pip download command

I want to download some pacakges(tensorflow, keras, imbalanced, xgboost, lightgbm, catboost) for centos 7.4 and python 3.7 on mac.How should i set platform name and ant other settings?I used below com…