Python:遍历两个列表,并将它们写到同一行的outfile中 [英] Python: iterate through two lists and write them to outfile on the same line

查看:81
本文介绍了Python:遍历两个列表,并将它们写到同一行的outfile中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时遍历两个列表,并将两个列表中的每个项目都写在同一行上,用制表符分隔.

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', 'marvelous']
pron = ['rVn', 'wIndl@s', 'mArv@l@s']

所需的输出:

run  rVn
windless  wIndl@s
marvelous  mArv@l@s

我尝试使用 zip ,但是它不允许我写文件:

I tried using zip but it doesn't let me write to file:

for w, p in zip(word, pron):
   outfile.write(w, p)

TypeError: function takes exactly 1 argument (2 given)

推荐答案

write仅将一个参数作为参数.要在同一行中写入两个变量,请更改:

write only takes one argument as a parameter. To write both variables in the same line, change:

outfile.write(w, p)

使得它是一个带有制表符和换行符的字符串:

such that it is one string with a tab and a newline:

outfile.write("{}\t{}\n".format(w,p))

这篇关于Python:遍历两个列表,并将它们写到同一行的outfile中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆