简体   繁体   English

如何在python中查找列表的序列号(如行号1、2)

[英]How to find sequence number of list in python (like line number 1, 2)

I want to find out the line number of the list, 我想找出列表的行号,

def creatList(file):
    try:
        for line in file:
            line=(line.rstrip()).split()
            rawList=[]
            rawList.append(line)
            creatRuleFile(rawList)

    finally:
        print("line(s) printed")

    def creatRuleFile(new):
        print(new)

inside creatRuleFile(rawList) function Im tring to display something similarly like this, 在creatRuleFile(rawList)函数内部,将显示类似的内容,

1. [a,b,c] 
2. [f,h,k] 
3. [b,s,y]

You can use a global variable (bad practice) for the counter, but a better method would be: 您可以对计数器使用全局变量(不好的做法),但是更好的方法是:

def creatList(file):
    try:
        for i, line in enumerate(file):
            ...
            creatRuleFile(i, rawList)
    ...

and

def creatRuleFile(i, new):
    print("{0}. {1}".format(i, new))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM