Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printing list/tuple elements on separate lines

10 views
Skip to first unread message

Johnny Chang

unread,
Jun 4, 2009, 8:37:42 PM6/4/09
to
I have a large list of strings that I am unpacking and splitting, and
I want each one to be on a new line. Someone showed me how to do it
and I got it working, except it is not printing each on its own
separate line as his did, making it incredibly hard to read. He did
it without adding a new line for anything. I can't get in touch with
him right now.

An example:

recs =
'asdfasdfasdfasdfasdf','asdfasdfasdfasdfasdf','asdfasdfasdfasdfasdf'
[(rec.split('f')) for rec in recs]

output:

[['asd', 'asd', 'asd', 'asd', 'asd', ''], ['asd', 'asd', 'asd', 'asd',
'asd', ''], ['asd', 'asd', 'asd', 'asd', 'asd', '']]

desired output:

[['asd', 'asd', 'asd', 'asd', 'asd', '']
['asd', 'asd', 'asd', 'asd', 'asd', '']
['asd', 'asd', 'asd', 'asd', 'asd', '']]

John Yeung

unread,
Jun 5, 2009, 12:36:05 AM6/5/09
to
On Jun 4, 8:37 pm, Johnny Chang <johnny...@gmail.com> wrote:
> I have a large list of strings that I am unpacking
> and splitting, and I want each one to be on a new line.
>
> An example:
>
> recs =
> 'asdfasdfasdfasdfasdf','asdfasdfasdfasdfasdf','asdfasdfasdfasdfasdf'
> [(rec.split('f')) for rec in recs]
>
> output:
>
> [['asd', 'asd', 'asd', 'asd', 'asd', ''], ['asd', 'asd', 'asd', 'asd',
> 'asd', ''], ['asd', 'asd', 'asd', 'asd', 'asd', '']]
>
> desired output:
>
> [['asd', 'asd', 'asd', 'asd', 'asd', '']
> ['asd', 'asd', 'asd', 'asd', 'asd', '']
> ['asd', 'asd', 'asd', 'asd', 'asd', '']]

Your friend may have used pprint:

>>> from pprint import pprint
>>> pprint(recs)


[['asd', 'asd', 'asd', 'asd', 'asd', ''],
['asd', 'asd', 'asd', 'asd', 'asd', ''],
['asd', 'asd', 'asd', 'asd', 'asd', '']]

John

Daniel Fetchinson

unread,
Jun 5, 2009, 12:44:07 AM6/5/09
to Python
>> I have a large list of strings that I am unpacking
>> and splitting, and I want each one to be on a new line.
>>
>> An example:
>>
>> recs =
>> 'asdfasdfasdfasdfasdf','asdfasdfasdfasdfasdf','asdfasdfasdfasdfasdf'
>> [(rec.split('f')) for rec in recs]
>>
>> output:
>>
>> [['asd', 'asd', 'asd', 'asd', 'asd', ''], ['asd', 'asd', 'asd', 'asd',
>> 'asd', ''], ['asd', 'asd', 'asd', 'asd', 'asd', '']]
>>
>> desired output:
>>
>> [['asd', 'asd', 'asd', 'asd', 'asd', '']
>> ['asd', 'asd', 'asd', 'asd', 'asd', '']
>> ['asd', 'asd', 'asd', 'asd', 'asd', '']]

By slightly modifying your requirements this might be good too:

print '\n'.join( [ 'aaaaaa', 'bbbbbbb', 'cccccccc', 'dddddddd', 'eeeeeeee' ] )


Cheers,
Daniel


--
Psss, psss, put it down! - http://www.cafepress.com/putitdown

0 new messages