gpt4 book ai didi

python - 将Python列表写入文件,项目之间用分号分隔

转载 作者:行者123 更新时间:2023-12-01 05:00:26 24 4
gpt4 key购买 nike

source= numpy.array([8,9])
sink= numpy.array([7,8])
pop_percentage= numpy.array([50,70,85])
buses= numpy.array([100,150,200])
bus_capacity= numpy.array([60,90])
a=[source,sink,pop_percentage,bus_capacity,buses,['mat1','mat2']]
file = open("/home/deep/Desktop/DATA/First_Data_File.txt", "w")
for list in itertools.product(*a):
for line in list:
print>>file,list[0],list[1],list[2]

我已经编写了上面的代码,并且在文件中得到了以下格式:

8 7 50

我需要使用分号分隔项目,如下所示:

8;7;50

请建议最Pythonic的方式来获得这个。提前致谢。

最佳答案

print>>file, ";".join([str (l) for l in list[:3]])

关于python - 将Python列表写入文件,项目之间用分号分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26343004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com