山月照归人 2022-04-12 18:00 采纳率: 70%
浏览 99
已结题

python中如何只获取for循环的最后一个列表?

用python写代码的时候,每一步循环都输出了一行列表,如何才能只保留或显示最后一行列表呢?

问题相关代码:
import os
path="E:\Game"
files=os.listdir(path)
y=[]
for file in files:
position=path+'\'+file
file=open(position)
read=file.read()
count=read.count('1')
fc=count/2500
y.append(fc)
print(y)
结果:
[0.494]
[0.494, 0.5132]
[0.494, 0.5132, 0.5132]
[0.494, 0.5132, 0.5132, 0.5132]
[0.494, 0.5132, 0.5132, 0.5132, 0.5132]

如何使结果只显示最后一行:[0.494, 0.5132, 0.5132, 0.5132, 0.5132]

  • 写回答

2条回答 默认 最新

  • 不会长胖的斜杠 后端领域新星创作者 2022-04-12 18:06
    关注

    print缩进一下即可

    import os
    path="E:\Game"
    files=os.listdir(path)
    y=[]
    for file in files:
      position=path+'\'+file
      file=open(position)
      read=file.read()
      count=read.count('1')
      fc=count/2500
      y.append(fc)
    print(y)
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月20日
  • 已采纳回答 4月12日
  • 创建了问题 4月12日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效