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
Like the title says, can I slice a sentence while ignoring whitespaces in Python?
For example, if the last letter of my word is sliced, the second letter of the following word needs to be sliced (while I'm using [::2]). I also have to preserve punctuations, so split isn't really an option. Replacing whitespaces isn't an option either, because I would have no way to put them back in the correct spot.
Sample input:
Myevmyozrtilets gwaaarkmv yuozub ubpi farfokm ctbhpe pientsfiydqe. zBmuvtk tahgelyu anlpsmo ttzevagrk yioquj awpyaoryts.
Expected output:
Memories warm you up from the inside. But they also tear you apart.
Sample implementation below.
Takes in consideration the punctuation (it looks like you've got it apart of the whitespace).
You'd enjoy trying to implement it on your own, I'm sure.
f="Myevmyozrtilets gwaaarkmv yuozub ubpi farfokm ctbhpe pientsfiydqe. zBmuvtk tahgelyu anlpsmo ttzevagrk yioquj awpyaoryts."
def g(f):
c=0
for l in f:
if l not in string.ascii_letters:
yield l
else:
if c%2==0:
yield l
c+=1
''.join(g(f))
'Memories warm you up from the inside. But they also tear you apart.'
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed yesterday.
Improve this question
I got an interesting assignment not too long ago, which was to make a regex that would take in a word. It would accept any word made from characters in the word, but never a word that had the original word in it.
Example:
Original string: "adam"
Strings it accepts: "ada", "a", "adm" "adaam", "amda"
Strings it doesn't accept: "adam" "aaadam" "amdadam"
I've been on this for a longer period of time and can't figure it out. Tried making it into an DFA (Deterministic finite automata) as well in the beginning hoping it would make it easier for me to figure it out when I see all the transitions.
Any ideas?
Here I made a regex for it:
https://regex101.com/r/eC2dwx/1
^((?!adam)[adam])+$
Explenation:
^ matches the start of a str and $ the end, so must be string
()+ matches the group one or more times
(?!adam) Not adam
[adam] Any letter of adam
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 4 months ago.
Improve this question
I have a list of lists, and I'm attempting to loop through and check to see if the strings in a specific index in each of the inner lists contain a combination of "XY" and then 4 numbers immediately following. The "XY" could be in various locations of the string, so I'm struggling with the syntax beyond just using "XY" in row[5]. How to I add the digits after the "XY" to check? Something that combines the "XY" and isdigit()? Am I stuck using the find function to return an index and then going from there?
You can use Python's regex module re with this pattern that matches XY and then four digits anywhere in the string.
import re
pattern = r'XY\d{4}'
my_list = [['XY0'],['XY1234','AB1234'],['XY1234','ABC123XY5678DEF6789']]
elem_to_check = 1
for row in my_list:
if len(row) > elem_to_check:
for found in re.findall(pattern, row[elem_to_check]):
print(f'{found} found in {row[elem_to_check]}')
Output:
XY5678 found in ABC123XY5678DEF6789
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm making an AI, and I want it to be able to search things. I don't know how to do this efficiently though.
Edit again: I fixed it on my own.
Instead of taking the second word inside of a list which contains every word the output has and added it to a search query. I took the output and removed the first word from it (which is "find", or "search") and then it will add that to the search query. Thanks for all the help.
If that's really all you want:
words = output.split(' ', 1)
if words[0] == 'search' and words[1] in Terms:
print("win")
For starters, you could forget about that long if condition and try this:
Terms = ['music','gaming','Minecraft','dank desmondo']
output = "search music"
if output.split()[1] in Terms:
print(True)
else:
print(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 2 years ago.
Improve this question
Example:
Input : "Base derived derived You are great"
Output: "Base->derived:derived You are great"
Here, the first two spaces are replaced with -> and : respectively, the rest of the string remains the same.
Nothing fancy, but if it's a one-off application, this would do the trick, using the Python str.split() method to create a list, splitting the string into three chunks on the first two spaces, then create a new string with those three chunks separated by '->' and ':'.
your_string = "Base derived derived You are great"
split_string = your_string.split(maxsplit=2)
result = f"{split_string[0]}->{split_string[1]}:{split_string[2]}"
As suggested in comments by #yatu, this can be reduced to a single statement using the *-operator to unpack the list:
result = '{}->{}:{}'.format(*your_string.split(maxsplit=2))
result in both cases:
'Base->derived:derived You are great'
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
Im looking for a way to be able to calculate the mean number of letters per each sentence in my list. Im trying to split the strings by white space and then count the length of each word inside but im not able to.
Any guidance would be helpful.
I am not going to write the code for you, but this will probably help. If I am understanding the question correctly you are trying to take a big block of text (paragraph) split it into sentences then get the average len of characters in each sentence.
So:
1) Break text block into sentences. Here is a post that should help you do that.
2) Count the letters in a sentence. Here is another post that will help remove the whitespace from the sentence. If you need to remove all the punctuation (everything except letters) from the string check out this post. Now you have a string that you can simply do a len(sentence_string) to get how many characters are in the sentence.
Next time please post the code you have tried, the errors you have gotten, and the text of the data you are trying to use. DON'T post pictures of words. It makes it a lot harder to help when we can't just copy and paste everything and debug it ourselves.
This code would work:
word = sentence.split(' ')
total = 0
for i in word:
total += len(i)
return float(total)/len(word)
The code splits on whitespace, then adds the length of the words and divides by the number of the words in the sentence. This calculates the average.