简体   繁体   中英

Write a single list to column

Using Python 2.7, i have a simple list where each element is composed by two numbers [(1,2),(3,4),(5,6)] that i print on a output txt file, but it seems like this:

[(1,2),(3,4),(5,6)]

I want to print the list on a single column like this:

(1,2)
(3,4)
(5,6)

Can someone help me with this?

Print element by element and not the entire list. Printing in a file is just changing the print function for writing in a file. Like this:

>>> f = open('workfile.txt', 'w')
>>> l = [(1,2),(3,4),(5,6)]
>>> for x in l:
...     f.write(str(x) + '\n')
...

And then if you look in workfile.txt with:

cat workfile.txt

You'll see:

(1, 2)
(3, 4)
(5, 6)

Use a loop.

>>> mylist = [(1,2),(3,4),(5,6)]
>>> for i in mylist:
...     print i
...
(1, 2)
(3, 4)
(5, 6)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM