Python Forum
Combining two strings together (not concatenation) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Combining two strings together (not concatenation) (/thread-17098.html)



Combining two strings together (not concatenation) - DreamingInsanity - Mar-28-2019

Take these two strings: '1000004000001' and '100000000333331'
When combined, they should become '1000000400333331'. Of course, the number '3's can on be on the left and right. Also the four should be overwritten if the threes take up too much space.

I currently have this:
print('here!')
for i in range(0,len(other)):
    if other[i:i+1] == '3':
        pos = other.index(other[i])
        break
if pos <= curr.index('4'):
    temp = True
new = new + '1'
count = list(other).count('3')
char_pos = curr.index('4')
if char_pos != 1:
    for i in range(0,len(curr)-char_pos):
        new = new + '0'
if temp == False:
    print('jhh')
    new = new + '4'
for j in range(0,count):
    new = new + '3'
new = new + '1'
'curr' is '1000004000001' and 'other' is a variation of '100000003333331'.

Hope this makes sense.
Dream


RE: Combining two strings together (not concatenation) - ichabod801 - Mar-28-2019

It doesn't make sense. The first string is 13 characters long, and the second string is 15 characters long. The final string is 16 characters long. How do you know how long the final string should be, or where the 4 should be in it?


RE: Combining two strings together (not concatenation) - DreamingInsanity - Mar-28-2019

(Mar-28-2019, 05:50 PM)ichabod801 Wrote: It doesn't make sense. The first string is 13 characters long, and the second string is 15 characters long. The final string is 16 characters long. How do you know how long the final string should be, or where the 4 should be in it?

Sorry, that was a mistake, they should all be 15 characters long. Where the 4 is placed depends on what is reteurned from the previous functions. It isn't random it just moves left or right in the string. (My previous thread is pretty much what it is.)
For now, leave it in the centre of the string.

Dream


RE: Combining two strings together (not concatenation) - ichabod801 - Mar-28-2019

Then I think you are really over complicating this. When the second string has a 3, the combined string should have a 3. Otherwise the combined string should have what the first string has.

So loop through the indexes. If the second string's character at that index is '3', add '3' to the combined string. Otherwise take whatever is at that index in the current string, and add that to the combined string.

Really, it would be better to do this by zipping together the two strings, but I'm trying to keep it basic.


RE: Combining two strings together (not concatenation) - DreamingInsanity - Mar-28-2019

Thanks for the reply.

While I am trying out the method suggeseted, would you please elaborate on 'zipping strings'. Although it might be too complicated for this, I would still like to learn something new that will come in handy in the future.

Dream


RE: Combining two strings together (not concatenation) - ichabod801 - Mar-28-2019

Zip takes two or more sequences, and gives a tuple of the first item from each sequence, then a tuple of the second item from each sequence, and so on:

chars = 'abc'
nums = '123'
for char, num in zip(chars, nums):
    print(char + num)
Output:
a1 b2 c3
It is the pythonic way to go through two lists together, rather than going by indexes.


RE: Combining two strings together (not concatenation) - DreamingInsanity - Mar-29-2019

Thanks!

That makes many things easier.

I created this piece of code with the zip function:
for char in zip(curr, other):
    if char[0] == '1' and char[1] == '1':
        new = new + '1'
    if char[0] == '0' and char[1] == '0':
        new = new + '0'
    if char[0] == '4' and char[1] != '3':
        new = new + '4'
    if char[1] == '3':
        new = new + '3'
print(new)
It probably could be improved but at least it works.

Thanks,
Dream