简体   繁体   English

如何从列表列表中获取整数

[英]How to get integer from list of lists

I have this list of lists:我有这个列表列表:

lst =[[5,5,5,5],[5,0,1,5],[5,0,3,5],[5,2,3,5],[5,5,5,5]]

And I need some kind of function for search one particular number (eg1) where the output would be something like this:我需要某种函数来搜索一个特定的数字(例如1),其中输出将是这样的:

 >>> It's in a list with index 1
 >>> In list with index 1, it has index 2 

Or even something more simple like:或者甚至更简单的东西,比如:

>>> 1
>>> 2

Thank you very much in advance!非常感谢您提前!

Here's a version that's a bit more concise and seems more practical to me:这是一个更简洁的版本,对我来说似乎更实用:

def find_int(l, target):
    sub_list = [x for x in range(len(l)) if target in l[x]][0]
    return sub_list, l[sub_list].index(target)


>>> lst = [[5,5,5,5],[5,0,1,5],[5,0,3,5],[5,2,3,5],[5,5,5,5]]
>>> print(find_int(lst,2))
(3, 1)

Here's a version with the cute text that returns exactly like you specified:这是一个带有可爱文本的版本,它完全按照您指定的方式返回:

def find_int(l, target):
    for sub_list in range(len(l)):
        if target in l[sub_list]:
            print("It's in a list with index " + str(sub_list))
            print("In list with index " + str(sub_list) + ", it has index " + str(l[sub_list].index(target)))
            return

>>> lst = [[5,5,5,5],[5,0,1,5],[5,0,3,5],[5,2,3,5],[5,5,5,5]]
>>> find_int(lst,2)
It's in a list with index 3                                                                                                                    
In list with index 1, it has index 1

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

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