0

I have three list A,B and C. I want to write the contents of the list to the file like this: A[1] B[1] C[1]

A[2] B[2] C[2] . . .

2
  • Do you know for sure that these lists will have same number of elements? You probably want to serialize or save objects as CSV? Commented Mar 4, 2011 at 19:19
  • I agree with @Daniel, some code snippet will help. I also think that @wheaties answered the question. Commented Mar 4, 2011 at 19:24

4 Answers 4

3

Take a look at using zip. With zip you get a list of tuples, the tuples being the ith item of each list. As a bonus, the list of tuples is truncated to the shortest list of the three:

myComboList = zip(A, B, C)

Then you can always write the things in the order you'd like them without fear that one list may be shorter/longer than any of the others.

Sign up to request clarification or add additional context in comments.

1 Comment

It works but now how can I print it without ['u, because in a list,([u' added before the element. Can I use a for loop on this zip list?
2

I assume that the lengths of all lists are the same,

assert len(A) == len(B) == len(C)
for a, b, c in zip(A, B, C):
    print a, b, c    # replace with file write

Comments

1

If your lists are long, itertools.izip() will probably be your friend. File object .writelines() can consume the list, or you can insert a yield construct in between to do the formatting.

def format(seq):
  for l in seq:
    yield "%s %s %s" % l
f.writelines(format(itertools.izip(A, B, C)))

2 Comments

Poor idea IMO to create a specific function format() while there's a built-in one. But +1 to associate writelines() and itertools.izip()
Ah, format() was backported to 2.6; I hadn't noticed. Then eyquem below has the better solution.
1

Taking idea of itertools.izip() from Bittrance ,

plus the fact that a built-in function format() already exists :

import itertools

A = ['arez','hgjkhg','jhdfg','uhireug']
B = ['aaa','bbb','cccc','ddd']
C = ['XXXX','YYYY','ZZZZ','WWWWW']

with open('zorgl.txt','w') as f:
    f.writelines("[{0}] [{1}] [{2}]\n".format(*tu)
                 for tu in itertools.izip(A, B, C))

result in the file

[arez] [aaa] [XXXX]
[hgjkhg] [bbb] [YYYY]
[jhdfg] [cccc] [ZZZZ]
[uhireug] [ddd] [WWWWW]

And a revelation happened to me:

I had never realized that writelines() writes a sequence of strings, which can be an iterable, while write() writes only one string

Until now I was doing this kind of thing:

f.write('\n'.join(sequentia))

to write in a file.

But '\n'.join(sequentia) is an object that is created before being written in one time. I believe.

Comparatively, writelines() can receive an iterable, then the writing of lines can be done one line at a time. This allows to write progressively a big quantity of data in a file, while it could be harder to write the total amount of data in only one chunk at one time.

Am I right ?

The only little defect of writelines() is that it writes the strings as they are, without adding a newline.

So writelines(sequentz) writes the same as write(''.join(sequentz)) . Only write('\n'.join(sequentz)) adds a newline between elements

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.