【问题标题】:How to solve this program using enumerate? [duplicate]如何使用枚举解决这个程序? [复制]
【发布时间】:2015-11-02 10:10:16
【问题描述】:
  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]

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

我的意思是: 我有一个类似 [1,apple.,2,mango] 的列表,现在使用枚举我想在使用卷号搜索后完全打印记录 2.,mango。如果 roll==2 则打印记录。

【问题讨论】:

  • 您有一个list,其中所有其他项目都是代表索引的整数?

标签: python file record enumerate


【解决方案1】:

最好这样使用:

f=open('Student.dat','r+') # opens Student.dat file
roll1= int(input("Enter roll to be found")) # need to use int to convert to integer type
for i,lst in enumerate(f):
    if i == roll1:
        print lst

【讨论】:

  • 它不工作...我没有得到任何输出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
相关资源
最近更新 更多