Python Forum
Combining two strings together (not concatenation)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combining two strings together (not concatenation)
#1
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
Reply
#2
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries i sawtooth500 3 966 Mar-22-2024, 03:08 AM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,757 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  String concatenation in SQL update statement hammer 3 1,504 Feb-24-2022, 08:00 PM
Last Post: hammer
  f string concatenation problem growSeb 3 2,246 Jun-28-2021, 05:00 AM
Last Post: buran
  Concatenation ?? ridgerunnersjw 1 1,708 Sep-26-2020, 07:29 PM
Last Post: deanhystad
  Finding multiple strings between the two same strings Slither 1 2,514 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Regarding concatenation of a list to a string Kedar 2 22,781 Aug-19-2018, 12:57 PM
Last Post: ichabod801
  lists, strings, and byte strings Skaperen 2 4,215 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020