Find the index of the second occurrence of a string inside a list

后端 未结 3 1449
耶瑟儿~
耶瑟儿~ 2021-01-20 18:31

This is my list and code:

x=[[\"hi hello\"], [\"this is other\"],[\"this\"],[\"something\"],[\"this\"],[\"last element\"]]
for line in x:
    y=x.index(line)         


        
3条回答
  •  醉话见心
    2021-01-20 19:17

    You could use enumerate(...) here.

    >>> x=[["hi hello"], ["this is other"],["this"],["something"],["this"],["last element"]]
    >>> for index, line in enumerate(x):
            print index, line
    
    
    0 ['hi hello']
    1 ['this is other']
    2 ['this']
    3 ['something']
    4 ['this']
    5 ['last element']
    

提交回复
热议问题