在Python中将2个暗淡数组“列表列表”输出到文本文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Python中将2个暗淡数组“列表列表”输出到文本文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2724字,纯文字阅读大概需要4分钟。
内容图文
![在Python中将2个暗淡数组“列表列表”输出到文本文件](/upload/InfoBanner/zyjiaocheng/687/0e15be973b4c4c3b87687e3f880e57ae.jpg)
一个简单的问题-我正在使用下面代码中的列表创建一个两个昏暗的数组(ddist = [[[0] * d for _ in [0] * d]).它使用gis数据输出距离.我只想要一种简单的方法来获取数组/列表的结果并将其输出到保持相同N * N结构的文本文件中.我过去使用过print语句的输出,但在这种情况下不是一个好的解决方案.
我是通过SAS刚接触python的.
def match_bg():
#as the name suggests this function will match the variations of blockgroups with grid travel time. Then output into two arras time and distance.
count = -1
countwo = -1
ctime = -1
ddist = [[0]*d for _ in [0]*d] #cratesan N*N array list
dtime = -1
while count < 10:
count = count +1
#j[count][7] = float(j[count][7])
#j[count][6] = float(j[count][6])
while countwo < d:
countwo = countwo+1
if count < 1:
#change values in bg file
j[countwo][7] = float(j[countwo][7])
j[countwo][6] = float(j[countwo][6])
#print j[count], j[countwo]
while ctime < RowsT:
#print ctime, lenth, t[ctime][0], count, countwo
ctime = ctime + 1
#takes both verations of big zone which should be end of the file and matches to travetime file - note 0 and 1 for t[] should be same for different files
if ((j[count][lenth-1] == t[ctime][0]) and (j[countwo][lenth-1] == t[ctime][1])) or ((j[countwo][lenth-1] == t[ctime][0]) and (j[count][lenth-1] == t[ctime][1])):
if t[ctime][0] != t[ctime][1]:
#jkdljf
x1=3963*j[count][7]*(math.pi/180)
x2=3963*j[countwo][7]*(math.pi/180)
y1=math.cos(j[count][6]*math.pi/180)*3963*j[count][7]*(math.pi/180)
y2=math.cos(j[countwo][6]*math.pi/180)*3963*j[countwo][7]*(math.pi/180)
dist=math.sqrt(pow(( x1-x2), 2) + pow((y1-y2), 2))
dtime = dist/t[ctime][11]
print countwo, count
ddist[count-1][countwo-1] = dist/t[ctime][lenth]
print dtime, "ajusted time", "not same grid"
print
elif j[count][5] != j[countwo][5]:
#ljdkjfs
x1=3963*j[count][7]*(math.pi/180)
x2=3963*j[countwo][7]*(math.pi/180)
y1=math.cos(j[count][6]*math.pi/180)*3963*j[count][7]*(math.pi/180)
y2=math.cos(j[countwo][6]*math.pi/180)*3963*j[countwo][7]*(math.pi/180)
dist=math.sqrt(pow(( x1-x2), 2) + pow((y1-y2), 2)) # could change to calculation
dtime = (dist/.65)/(t[ctime][10]/60.0)
print dtime, dist, "not in the same bg", j[count], j[countwo], t[ctime]
elif j[count][5] == j[countwo][5]:
if t[count][7] < 3000000:
dtime = 3
elif t[count][7] < 20000000:
dtime = 8
else:
dtime = 12
print dtime, "same bg"
print t[ctime][0], t[ctime], 1, j[count], j[countwo]
else:
print "error is skip logic", j[count], j[countwo], t[ctime]
break
#elif (j[countwo][lenth-1] == t[ctime][0]) and (j[count][lenth-1] == t[ctime][1]):
#print t[ctime][0], t[ctime], 2, j[count], j[countwo]
#break
ctime = -1
countwo = -1
解决方法:
这就是您可以输出二维列表(或与此相关的任何二维列表)的方式:
with open(outfile, 'w') as file:
file.writelines('\t'.join(str(j) for j in i) + '\n' for i in top_list)
内容总结
以上是互联网集市为您收集整理的在Python中将2个暗淡数组“列表列表”输出到文本文件全部内容,希望文章能够帮你解决在Python中将2个暗淡数组“列表列表”输出到文本文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。