4

I need a way to copy all of the positions of the spaces of one string to another string that has no spaces.

For example:

string1 = "This is a piece of text"
string2 = "ESTDTDLATPNPZQEPIE"

output = "ESTD TD L ATPNP ZQ EPIE"
cs95
  • 379,657
  • 97
  • 704
  • 746
wkelly
  • 41
  • 4

3 Answers3

3

Insert characters as appropriate into a placeholder list and concatenate it after using str.join.

it = iter(string2)
output = ''.join(
    [next(it) if not c.isspace() else ' ' for c in string1]  
)

print(output)
'ESTD TD L ATPNP ZQ EPIE'

This is efficient as it avoids repeated string concatenation.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • why `.isspace()`? very clever though :) – Joe Iddon Mar 25 '18 at 20:48
  • @JoeIddon cleaner than `c == ' '` imo, and thank you. I like your generator solution, it was the first thing I came up with myself. – cs95 Mar 25 '18 at 20:49
  • But in general, `==` is more extensible. Oh also, isn't the list-comp unnecessary as you can just use a generator if passing into`str.join`. – Joe Iddon Mar 25 '18 at 20:51
  • @JoeIddon Ah, that's a common gotcha with `str.join`. It is _faster_ to pass a list than a generator, because str.join will anyway make a second pass over it to create the list in memory. There is a source somewhere, I can find it. – cs95 Mar 25 '18 at 20:52
  • @JoeIddon You may peruse this [excellent answer](https://stackoverflow.com/a/9061024/4909087) by Raymond Hettinger. – cs95 Mar 25 '18 at 20:53
  • Very interesting! But what about the cases where you aren't using a comprehension as in my answer... Would it be faster there to pass into `list()` first. – Joe Iddon Mar 25 '18 at 21:00
  • Also, when Raymond says: "If you give it one, it can start its work immediately. If you give it a genexp instead, it cannot start work until it builds-up a new list", if you have to create the list yourself, then I get that this would save time for the `str.join` call, but you have lost all the time you saved by creating a list (over a generator). I do agree that it is faster though now I have checked; is it simply the overhead of closing a generator? – Joe Iddon Mar 25 '18 at 21:03
2

You need to iterate over the indexes and characters in string1 using enumerate().

On each iteration, if the character is a space, add a space to the output string (note that this is inefficient as you are creating a new object as strings are immutable), otherwise add the character in string2 at that index to the output string.

So that code would look like:

output = ''
si = 0
for i, c in enumerate(string1):
    if c == ' ':
         si += 1
         output += ' '
    else:
         output += string2[i - si]

However, it would be more efficient to use a very similar method, but with a generator and then str.join. This removes the slow concatenations to the output string:

def chars(s1, s2):
    si = 0
    for i, c in enumerate(s1):
        if c == ' ':
            si += 1
            yield ' '
        else:
            yield s2[i - si]
output = ''.join(char(string1, string2))
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
0

You can try insert method :

string1 = "This is a piece of text"
string2 = "ESTDTDLATPNPZQEPIE"


string3=list(string2)

for j,i in enumerate(string1):
    if i==' ':
        string3.insert(j,' ')
print("".join(string3))

outout:

ESTD TD L ATPNP ZQ EPIE
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88