Python: iterate through two lists and write them to outfile on the same line

前端 未结 3 1665
时光说笑
时光说笑 2021-01-24 21:33

I would like to iterate through two lists simultaneously and write each item from both lists, tab-separated on the same line.

word = [\'run\', \'windless\', \'ma         


        
3条回答
  •  渐次进展
    2021-01-24 21:48

    If you want to make life easier for yourself you can use the print statement/function.

    Python 2:

    print >>outfile, w, p
    

    Python 3 (or Python 2 using from __future__ import print_function at the top):

    print(w, p, file=outfile)
    

    This way you can avoid manually adding the '\n' or converting everything to a single string.

提交回复
热议问题