Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
I want to replace a word in a string which is determined by the word entered by the user.
For example, if user enters "pot", the code replaces it with "who" but if he enters "top" then it becomes "mam".
I have a set of 225 words and their replacements. I am using python for this. Replacing each word individually in a condition is really impractical. is their a quicker way to do this. I currently have no code as I am totally confused how to do this.
Using excel throws an error no matter what.
Yes, there is a very convenient way to do this; python dicts:
d = {"pot": "who", "top": "mam"}
print(d["pot"])
print(d["top"])
Output:
who
mam
In Python 3.1, you can use the new match - case statement:
s = "pot"
match s:
case "pot":
print("who")
case "top":
print("top")
case _:
print(None)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have a string that might have any of the following format (example) :
1111__1111
1111__1111_11
111_11A_11
I have added the following check :
import re
print(bool(re.match("\d__\d","1111_1111"))
print(bool(re.match("\d__\d_\d","1111_1111_11"))
print(bool(re.match("\d_\d[A-Za-z]_\d","111_11A_11"))
I don't think the regex is correct because when I introduce a character in the first regex for example it returns me True Always.
can you please point me to a solution?
Thank you
It returns True because the pattern is trying to find matches based on each one of the characters inside the pattern string.
The following regular expression finds exact matches for the three scenarios:
print(bool(re.match("(^\d{4}__\d{4}$)","1111__1111")))
print(bool(re.match("(^\d{4}\_\d{4}\_\d{2}$)","1111_1111_11")))
print(bool(re.match("(^\d{3}_\d{2}[A-Z]_\d{2}$)","111_11A_11")))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Id codes at a company come in the form x-y-zzzzzz, where x is a digit and y is a letter and zzzzzz represents a string of 6 letters. Write a function which takes in a code as an input (e.g. 3-a-abaabb) and returns the zzzzzz part (e.g. abaabb).
I have no idea how to start and solve this question. any help would be much appreciated. My IDE is pycharm (solving python coding problems) I basically need to create a function which takes the code as an input and will return the last 6 letters
You can use str.spit('-') then search count in code with repeat is equal or not, like below:
def fnd_code(code):
repeat, char, search = code.split('-')
return search.count(char) == int(repeat)
print(fnd_code('3-a-abaabb'))
print(fnd_code('4-a-abaabb'))
Output:
True
False
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm currently trying to perform this, but my book for class doesn't show anything in relation to Boolean variables or anything related to this problem. I've been looking online for a while and everything is way too complicated for something that should be simple according the unit. If the solution is posted here, what was typed in to find it? Maybe I need to rephrase my question.
https://i.stack.imgur.com/RMSEx.png
def getSecondWord(sentence):
res = sentence.split(' ')[1]
if res.endswith('.'):
return res[:-1]
else:
return res
inputSentence = 'broccoli is delicious'
secondWord = getSecondWord(inputSentence)
print("second word is:", secondWord)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Ideally, I'd like the program to erase/un-log any and all erroneous character(s) when the Backspace key is pressed and replace them with the correct characters.
After searching for solutions to no avail, I'm wondering if it's even possible? If it is, my guess is that the code needed to do this might involve the modules: 're', 'readchar', 'msvcrt', 'getch' or some combination of those, in addition to using 'string.replace', 'x.remove', 'r/R', 'raw_input' 'x.translate', or the like. But I don't have the knowledge or skills yet to figure out how to apply them.
This code may be what you are looking for:
import re
text = "Helll[Back Space]o how are yoo[Back Space]u"
result = list(text)
for (start, end) in [(m.start(), m.end()) for m in re.finditer('\[Back Space\]', text)]:
text = text.replace(''.join(result[start-1:end]), '')
print text
Output:
Hello how are you
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I've an issue here. I have a string and I want to extract parts of it using regex. Here is the string
{{name}} I love me some work {{hero}}
I want to extract
[{{name}}, {{hero}}]
also in a case where the string exist as
{{name} I love me some work {{hero, come in {here, this is right}
I still want to get
[{{name}, {{name, {here, right}]
I hope this makes sense. I am working with Python.
If you want ['{{name}}', '{{hero}}', '{{hero, come in {here, this is right}'] use #Avinash's regex.
If you want ['{{name}}', '{{hero}}', '{{hero', {here', 'right}'] use the following:
re.findall(r'{+\w+}*|{*\w+}+', s)
RegEX DEMO
Have you tried the following?
import re
s = '{{name} I love me some work {{hero, come in {here, this is right}'
print re.findall(r'\{.*?\}', s)