In Python, I want to add a space to each string in the list. The result is irrelevant whether it is a string or a list. But I would like to get the number of all cases where there can be spaces in the string.
first, I tried like this, Make string to list.
sent = 'apple is good for health'
sent.split(' ')
sentence = []
b = ''
for i in sent:
c = ''
for j in sent[(sent.index(i)):]:
c += j
sentence.append((b+' '+c).strip())
b += i
sentence
In this case, the result obtain a string that contains only one space.
I also tried
for i in range(len(sent)):
sent[i:i] = [' ']
and another try is used ' '.join(sent[i:])
but the results are same.
how can I get
'apple isgoodforhealth', 'appleis goodforhealth', 'appleisgood forhealth', 'appleisgoodfor health', 'appleis goodfor health', 'apple isgood forhealth', 'appleisgood forhealth' ...
like this?
I really want to get the number of all cases.
My take on the problem utilizing itertools.combinations.
import itertools
sent = 'apple is good for health'
sent = sent.split(' ')
# Get indices for spaces
N = range(len(sent) - 1)
for i in N:
# Get all combinations where the space suits
# Note that this doesn't include the option of no spaces at all
for comb in itertools.combinations(N, i + 1):
# Add space to the end of each word
# with index contained in the combination
listsent = [s + " " if j in comb else s for j, s in enumerate(sent)]
# Make the result a string or count the combinations if you like
tempsent = "".join(listsent)
print(tempsent)
Use Join:-
sent = 'apple is good for health'
sent = sent.split(' ')
start_index = 0
last_index = len(sent)
for i in range(len(sent)-1):
first_word = "".join(sent[start_index:i+1])
second_word = "".join(sent[i+1:last_index])
print(first_word, " ", second_word)
Hope the above code will give output your way i.e, 'apple
isgoodforhealth', 'appleis goodforhealth', 'appleisgood forhealth etc.
Related
I am looking for a way to add spaces between words in a string until len(string) < n is reached.
I've tried this:
string = "you are great"
n = 20
res = []
for i in string:
if ord(i) == 32 and len(string) < num:
res = [string + i]
And I want it to add spaces between "you" and "are", and "are" and "great.
So it gives me something like this:
res = ["you are great"]
But instead I get this
"you are great "
No need of loops:
string = "you are great"
n = 20
a = n - len(string.replace(' ', '')) # amount of spaces to add
b = string.split() # list of words (without spaces)
if a & 1:
b[0] += ' ' # if 'a' is odd, add an extra space to first word
d = (' ' * (a // 2)).join(b) # concatenate all words with 'a' spaces between them
print(d)
The output is:
"you are great"
You code is doing what it is suppose to do but as you can see in the second output it adds a space to your string. The code below is the issue.
Res = [string + i]
I'm trying to reformat a series of strings into Camel Case by returning the fragments from input as a single "sentence".
this is what I have so far -
def convert(s):
if(len(s) == 0):
return
s1 = ''
s1 += s[0].upper()
for i in range(1, len(s)):
if (s[i] == ' '):
s1 += s[i + 1].upper()
i += 1
elif(s[i - 1] != ' '):
s1 += s[i]
print(s1)
# Driver Code
def main():
s = "BusOneBusTWOBUSthree"
convert(s)
if __name__=="__main__":
main()
output I'm getting is -
Bus One
Bus TWO
BUS three
output I'm wanting to get is -
busOne busTwo busThree
It looks like your example s= won't get you the output you say you're getting, so I am guessing that your input is space separated. It looks like you want to alternatively join 2 words together and make the 2nd word's first letter Uppercase. If so, split the string into a list, then iterate through the list and add to your final string. Here's one way to do that:
original_string = 'buS one buS two bus thRee'
original_string = original_string.lower()
list_of_words = original_string.split()
# splits on space by default, or specify 'x' to split on the letter x
camel_case_output = ''
for i in range(len(list_of_words)):
this_word = list_of_words[i]
if i % 2 == 0: # even numbers
camel_case_output += list_of_words[i]
else:
this_word = this_word[0].upper() + this_word[1:]
camel_case_output += this_word + ' '
camel_case_output.strip() # remove the last space, if you ended with extra
I have a list with values say list =[Copper, Wood, Glass, Metal]
string = 'Wood table with small glass center,little bit of metal'
I need to search if specific values are available in my string but should ignore the least prominent values like glass and metal using nearby words.
I tried re.findall and I am getting output as Wood, Glass, Metal. How to ignore 'Glass' and 'Metal' in this case by using nearby keywords such as 'small' and 'little'.
Expected Output = [Wood]
My understanding: What I understand from your question is that you are trying to remove values from the list that follow words such as 'small' and 'little'.
Code:
lst = ['Copper', 'Wood', 'Glass', 'Metal']
string = 'Wood table with small glass center,little bit of metal'
keywords = ['small','little']
punc = '''!()-[]{};:'"\, <>./?##$%^&*_~'''
for ele in string:
if ele in punc:
string = string.replace(ele, " ")
lst = [stringg.lower() for stringg in lst]
string = string.lower()
lst = [word for word in lst if word.lower() in string.lower()]
words_lst = string.split(' ')
final = []
count = 0
for elem in lst:
count = 0
index = words_lst.index(elem)
slice_index = index - 4 if index - 4 >= 0 else 0
range_lst = words_lst[slice_index:index + 1]
for keyword in keywords:
if keyword not in range_lst and elem not in final:
count += 1
if count == len(keywords):
final.append(elem)
Output:
>>> final
['wood']
I have a very messy data I am noticing patterns where ever there is '\n' end of the element, it needs to be merged with single element before that.
sample list:
ls = ['hello','world \n','my name','is john \n','How are you?','I am \n doing well']
ls
return/tryouts:
print([s for s in ls if "\n" in s[-1]])
>>> ['world \n', 'is john \n'] # gave elements that ends with \n
How do I get it elements that ends with '\n' merge with 1 before element? Looking for a output like this one:
['hello world \n', 'my name is john \n', 'How are you?','I am \n doing well']
If you are reducing a list, maybe, one readable approach is to use reduce function.
functools.reduce(func, iter, [initial_value]) cumulatively performs an operation on all the iterable’s elements and, therefore, can’t be applied to infinite iterables.
First of all, you need a kind of struck to accumulate results, I use a tuple with two elements: buffer with concatenated strings until I found "\n" and the list of results. See initial struct (1).
ls = ['hello','world \n','my name','is john \n','How are you?','I am \n doing well']
def combine(x,y):
if y.endswith('\n'):
return ( "", x[1]+[x[0]+" "+y] ) #<-- buffer to list
else:
return ( x[0]+" "+y, x[1] ) #<-- on buffer
t=reduce( combine, ls, ("",[]) ) #<-- see initial struct (1)
t[1]+[t[0]] if t[0] else t[1] #<-- add buffer if not empty
Result:
['hello world \n', 'my name is john \n', 'How are you? ', 'I am \n doing well ']
(1) Explained initial struct: you use a tuple to store buffer string until \n and a list of already cooked strings:
("",[])
Means:
("__ buffer string not yet added to list __", [ __result list ___ ] )
I wrote this out so it is simple to understand instead of trying to make it more complex as a list comprehension.
This will work for any number of words until you hit a \n character and clean up the remainder of your input as well.
ls_out = [] # your outgoing ls
out = '' # keeps your words to use
for i in range(0, len(ls)):
if '\n' in ls[i]: # check for the ending word, if so, add it to output and reset
out += ls[i]
ls_out.append(out)
out = ''
else: # otherwise add to your current word list
out += ls[i]
if out: # check for remaining words in out if total ls doesn't end with \n
ls_out.append(out)
You may need to add spaces when you string concatenate but I am guessing that it is just with your example. If you do, make this edit:
out += ' ' + ls[i]
Edit:
If you want to only grab the one before and not multiple before, you could do this:
ls_out = []
for i in range(0, len(ls)):
if ls[i].endswith('\n'): # check ending only
if not ls[i-1].endswith('\n'): # check previous string
out = ls[i-1] + ' ' + ls[i] # concatenate together
else:
out = ls[i] # this one does, previous didn't
elif ls[i+1].endswith('\n'): # next one will grab this so skip
continue
else:
out = ls[i] # next one won't so add this one in
ls_out.append(out)
You can solve it using the regex expression using the 're' module.
import re
ls = ['hello','world \n','my name','is john \n','How are you?','I am \n doing well']
new_ls = []
for i in range(len(ls)):
concat_word = '' # reset the concat word to ''
if re.search(r"\n$", str(ls[i])): # matching the \n at the end of the word
try:
concat_word = str(ls[i-1]) + ' ' + str(ls[i]) # appending to the previous word
except:
concat_word = str(ls[i]) # in case if the first word in the list has \n
new_ls.append(concat_word)
elif re.search(r'\n',str(ls[i])): # matching the \n anywhere in the word
concat_word = str(ls[i])
new_ls.extend([str(ls[i-1]), concat_word]) # keeps the word before the "anywhere" match separate
print(new_ls)
This returns the output
['hello world \n', 'my name is john \n', 'How are you?', 'I am \n doing well']
Assuming the first element doesn't end with \n and all words are longer than 2 characters:
res = []
for el in ls:
if el[-2:] == "\n":
res[-1] = res[-1] + el
else:
res.append(el)
Try this:
lst=[]
for i in range(len(ls)):
if "\n" in ls[i][-1]:
lst.append((ls[i-1] + ' ' + ls[i]))
lst.remove(ls[i-1])
else:
lst.append(ls[i])
lst
Result:
['hello world \n', 'my name is john \n', 'How are you?', 'I am \n doing well']
What is the most efficient way to remove spaces from a text, and then after the neccessary function has been performed, re-insert the previously removed spacing?
Take this example below, here is a program for encoding a simple railfence cipher:
from string import ascii_lowercase
string = "Hello World Today"
string = string.replace(" ", "").lower()
print(string[::2] + string[1::2])
This outputs the following:
hlooltdyelwrdoa
This is because it must remove the spacing prior to encoding the text. However, if I now want to re-insert the spacing to make it:
hlool tdyel wrdoa
What is the most efficient way of doing this?
As mentioned by one of the other commenters, you need to record where the spaces came from then add them back in
from string import ascii_lowercase
string = "Hello World Today"
# Get list of spaces
spaces = [i for i,x in enumerate(string) if x == ' ']
string = string.replace(" ", "").lower()
# Set string with ciphered text
ciphered = (string[::2] + string[1::2])
# Reinsert spaces
for space in spaces:
ciphered = ciphered[:space] + ' ' + ciphered[space:]
print(ciphered)
You could use str.split to help you out. When you split on spaces, the lengths of the remaining segments will tell you where to split the processed string:
broken = string.split(' ')
sizes = list(map(len, broken))
You'll need the cumulative sum of the sizes:
from itertools import accumulate, chain
cs = accumulate(sizes)
Now you can reinstate the spaces:
processed = ''.join(broken).lower()
processed = processed[::2] + processed[1::2]
chunks = [processed[index:size] for index, size in zip(chain([0], cs), sizes)]
result = ' '.join(chunks)
This solution is not especially straightforward or efficient, but it does avoid explicit loops.
Using list and join operation,
random_string = "Hello World Today"
space_position = [pos for pos, char in enumerate(random_string) if char == ' ']
random_string = random_string.replace(" ", "").lower()
random_string = list(random_string[::2] + random_string[1::2])
for index in space_position:
random_string.insert(index, ' ')
random_string = ''.join(random_string)
print(random_string)
I think this might Help
string = "Hello World Today"
nonSpaceyString = string.replace(" ", "").lower()
randomString = nonSpaceyString[::2] + nonSpaceyString[1::2]
spaceSet = [i for i, x in enumerate(string) if x == " "]
for index in spaceSet:
randomString = randomString[:index] + " " + randomString[index:]
print(randomString)
string = "Hello World Today"
# getting index of ' '
index = [i for i in range(len(string)) if string[i]==' ']
# storing the non ' ' characters
data = [i for i in string.lower() if i!=' ']
# applying cipher code as mention in OP STATEMENT
result = data[::2]+data[1::2]
# inserting back the spaces in there position as they had in original string
for i in index:
result.insert(i, ' ')
# creating a string solution
solution = ''.join(result)
print(solution)
# output hlool tdyel wrdoa
You can make a new string with this small yet simple (kind of) code:
Note this doesn't use any libraries, which might make this slower, but less confusing.
def weird_string(string): # get input value
spaceless = ''.join([c for c in string if c != ' ']) # get spaceless version
skipped = spaceless[::2] + spaceless[1::2] # get new unique 'code'
result = list(skipped) # get list of one letter strings
for i in range(len(string)): # loop over strings
if string[i] == ' ': # if a space 'was' here
result.insert(i, ' ') # add the space back
# end for
s = ''.join(result) # join the results back
return s # return the result