0

if i have three arrays the first one is A,B,C,D and the second one is E,F,G,H and the last one is I,J,K,L i want to use this three array and make an output like this :

AEI
BFJ
CGK
DHL

i try this code

import re
array1 = 'A','B','C','D'
array2 = 'E','F','G','H'
array3 = 'I','J','K','L'
arys = [array1,array2,array3]

for a,b,c,d in arys:
    print a+b+c+d

it didnt work

how to make this work

1
  • 2
    tuple != array. What you are creating is a list of tuples. Tuples function very similarly to lists however they are immutable, which can be a subtle but important distinction. Commented Mar 25, 2016 at 23:13

3 Answers 3

4

Try this:

array1 = 'A','B','C','D'
array2 = 'E','F','G','H'
array3 = 'I','J','K','L'
for elems in zip(array1, array2, array3):
    print ''.join(elems)

It prints

AEI
BFJ
CGK
DHL

Edit: you could also just zip the 3 strings together instead of creating tuples and get the same output:

for elems in zip("ABCD", "EFGH", "IJKL"):
    print(''.join(elems))
Sign up to request clarification or add additional context in comments.

Comments

1

You can also do it with map in python2:

array1 = 'A','B','C','D'
array2 = 'E','F','G','H'
array3 = 'I','J','K','L'
print("\n".join(map("".join, map(None, array1, array2, array3))))
AEI
BFJ
CGK
DHL

Comments

1

Here is one simple way (you definitely want to use zip() here):

array1 = 'A','B','C','D'
array2 = 'E','F','G','H'
array3 = 'I','J','K','L'

for triplet in zip(array1, array2, array3):
    print ''.join(triplet)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.