Merge two strings with alternate chars as output

后端 未结 5 1291
悲哀的现实
悲哀的现实 2021-01-17 01:53

I got the task to alternately combine the letters of two strings with the same length.

For example:

Inputstring 1: \"acegi\"

Inputstring 2: \"bdfhj\         


        
相关标签:
5条回答
  • 2021-01-17 02:00

    One can simply use map with join to get desired result:

    "".join(map(lambda x,y: x+y, astr, bstr))
    

    zip in not needed here since 2 strings/lists can be provided to map which has a lambda function that takes 2 arguments. Also, + symbol works to concatenate 2 strings here.

    0 讨论(0)
  • 2021-01-17 02:04

    You can do it in one line using zip and join.

    out1 = ''.join(''.join(f for f in tup) for tup in zip(inp1, inp2))
    

    or the more functional-style:

    out1 = ''.join(map(''.join, zip(inp1, inp2)))  # kudos @Coldspeed
    

    which both print

    abcdefghij
    

    Braking the code down:

    • zip()

      pairs = list(zip(inp1, inp2))
      print(pairs )  # [('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h'), ('i', 'j')]
      
    • .join()

      temp = []
      for sub in pairs:
          temp.append(''.join(sub))
      print(temp)  # ['ab', 'cd', 'ef', 'gh', 'ij']
      
    • .join() again

      out1 = ''.join(temp)
      print(out1)  # abcdefghij
      

    Finally, and for your entertainment and learning only, two additional, more old-school approaches:

    out1 = ''
    for i in range(len(inp1)):
        out1 += inp1[i] + inp2[i]
    

    and

    out1 = ''
    for i, c in enumerate(inp1):
        out1 += c + inp2[i]
    
    0 讨论(0)
  • 2021-01-17 02:04

    You have several problems in your code. The one that is the most important is your naming problem. Give meaningful names to your variables.

    Now, to the logic. You need to iterate through your strings at the same time, or use an index on both at the same time, and not one after the other.

    You could do as such, for a more pythonic way of doing this.

    first = input("Your first string.")
    second = input("Your second string.")
    
    if len(first) != len(second):
        print 'Bad length for the inputs.'
    else:
        newString = ""
        for s1, s2 in zip(first, second):
            newString += s1 + s2
        print newString
    
    0 讨论(0)
  • 2021-01-17 02:06

    One of the first things you should learn when programming is to use meaningful variable names, not cryptic, 1-letter names.

    Your code is not alternating between the two input strings. You're looping through the first string, then looping through the second string, and never repeating.

    I'm not sure what the point of the c variable is. You set it to 1 at the beginning of the script, then add 1 to it later, but then the script ends. Was there supposed to be another loop around all that code?

    The loop that checks if f is in range(c, c+1) could just be f = c, there's no point to looping.

    The error is coming from

    x = x + f
    

    because x is a string and f is an int. I suspect you wanted to do x = x + s[f].

    The whole thing can be simplified greatly.

    string1 = input("Enter string 1: ")
    len1 = len(string1)
    string2 = input("enter string 2: ")
    len2 = len(string2)
    if len1 != len2:
        print("Inputs must be the same length")
    else:
        result = ""
        for i in range(len1):
            result += string1[i]
            result += string2[i]
        print(result)
    
    0 讨论(0)
  • 2021-01-17 02:09

    Get a and b with input or however you want. No need to copy that. The point is the code.

    a = 'acegi'
    b = 'bdfhj'
    
    c = ''
    for i in range(len(a)):
        c += a[i] + b[i]
    
    print(c)
    

    Result is:

    abcdefghij
    
    0 讨论(0)
提交回复
热议问题