python将输出保存为txt_将输出值保存在python列的txt文件中

1586010002-jmsa.png

My input value gives corresponding output values as;

my_list = []

for i in range(1,10):

system = i,i+2*i

my_list.append(system)

print(my_list)

[(1, 3)]

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

[(1, 3), (2, 6), (3, 9)]

[(1, 3), (2, 6), (3, 9), (4, 12)]

I want output stored in 2 columns; where 1,2,3is first column elements and 3,6,9 for 2nd column.

(1 3)

(2 6)

(3 9)

so on... and then write store these values in text file. For text file, is it possible to generate text file as a part of script? Thanks

解决方案

you need to save the element of my_list that has equal index with i-1 (your range began at 1):

my_list=[]

with open ('new.txt','w') as f:

for i in range(1,10):

system = i,i+2*i

my_list.append(system)

print(my_list)

f.write(str(my_list[i-1])+'\n')

output:

(1, 3)

(2, 6)

(3, 9)

(4, 12)

(5, 15)

(6, 18)

(7, 21)

(8, 24)

(9, 27)

also as says in comments you don't need my_list you can do it with following code :

with open ('new.txt','w') as f:

for i in range(1,10):

system = i,i+2*i

f.write(str(system)+'\n')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值