please help...writing set to a file

Y

yadin

Good day every one!

I got this python program that returns me a set like this..
Set ([‘A\n’, B\n’, ‘C\n’, ‘D\n’, ‘E\n’, ‘F\n’, ‘G\n’ ])
And a list pp = [‘100\n’ ‘200\n’ ‘300\n’ ‘400\n’]
I was reading this from a file….
How can I transform this to something that looks like this
Column1 Column 2

100 A
200 B
300 C
400 D
E
F
G
And then write this to a file???
thank you for taking your time!!!
 
D

Dave Angel

yadin said:
Good day every one!

I got this python program that returns me a set like this..
Set ([‘A\n’, B\n’, ‘C\n’, ‘D\n’, ‘E\n’, ‘F\n’, ‘G\n’ ])
And a list pp =‘100\n’ ‘200\n’ ‘300\n’ ‘400\n’]
I was reading this from a file….
How can I transform this to something that looks like this
Column1 Column 2

100 A
200 B
300 C
400 D
E
F
G
And then write this to a file???
thank you for taking your time!!!
Since a set has no order, it cannot be done. There's no association
between 100 and A that can be independently determined from just the set
and the list.

Just how was the original assignment worded, anyway? And what version of
Python are you using for it?

After you solve that problem (perhaps by returning a list in both
cases), then the real question might be one of formatting.


Since the columns in your example don't line up in any meaningful way,
perhaps you just want to slap a few spaces before and between the
elements. You'll need to do a strip() on the pp items, to get rid of the
newline. Then something like:
line = " " + ppitem.strip() + " " + otheritem

Loop through the lists and send the lines to the file. Don't forget to
close it at the end.
 
A

Alan G Isaac

I got this python program that returns me a set like this..
Set ([‘A\n’, B\n’, ‘C\n’, ‘D\n’, ‘E\n’, ‘F\n’, ‘G\n’ ])
And a list pp = [‘100\n’ ‘200\n’ ‘300\n’ ‘400\n’]
I was reading this from a file….
How can I transform this to something that looks like this
Column1 Column 2

100 A
200 B
300 C
400 D
E
F
G
And then write this to a file???


http://docs.python.org/library/functions.html#sorted

http://docs.python.org/library/itertools.html#itertools.izip_longest

http://docs.python.org/library/stdtypes.html#str.format

http://docs.python.org/library/functions.html#open

hth,
Alan Isaac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top