如何在这个程序中使用枚举? [英] How to use enumerate in this program?

查看:59
本文介绍了如何在这个程序中使用枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

f=open('Student.dat','r+') # opens Student.dat file
roll1=input("Enter roll to be found") # to find a record in a list using a roll no
rec=f.readlines()
for i,lst in enumerate(rec):
        if lst == roll1:
            print rec[i]

这是使用枚举的正确方法吗??还是我应该在其中使用另一个循环?

Is this the proper way to use enumerate?? or should i use another loop within??

推荐答案

这里 enumerate 没有多大帮助;你可以改用(这会更简单更清晰):

Here enumerate doesn't help much; you could use instead (which would be simpler and clearer):

for i in rec:
    if i == roll1:
        print i

enumerate 在您确实需要同时获取值和索引时很有用,但这里似乎并非如此.(在你的代码段 rec[i]lst 做同样的事情;因此如果你真的想使用 enumerate 你应该替换它.)

enumerate is useful when you really need to get at the same time values and indices, which doesn't seem to be the case here. (In your piece of code rec[i] does the same thing than lst; thus if you really want to use enumerate you should replace it.)

顺便说一句,你应该为你的问题添加更好的标签,至少是 python.

By the way, you should add better tags to your question, at least python.

这篇关于如何在这个程序中使用枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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