在 python 的嵌套列表中查找元素的索引
Finding the index of an element in nested lists in python
我正在尝试获取 python 中嵌套列表中元素的索引 - 例如 [[a, b, c], [d, e, f], [g,h]]
(并非所有列表的大小都相同)。
我试过使用
strand_value= [x[0] for x in np.where(min_value_of_non_empty_strands=="a")]
但这只返回一个空列表,即使该元素存在。知道我做错了什么吗?
def find_in_list_of_list(mylist, char):
for sub_list in mylist:
if char in sub_list:
return (mylist.index(sub_list), sub_list.index(char))
raise ValueError("'{char}' is not in list".format(char = char))
example_list = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
find_in_list_of_list(example_list, 'b')
(0, 1)
假设你的列表是这样的:
lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g','h']]
list_no = 0
pos = 0
for x in range(0,len(lst)):
try:
pos = lst[x].index('e')
break
except:
pass
list_no = x
list_no
给出列表编号,pos
给出列表中的位置
您可以使用列表理解和枚举来做到这一点
代码:
lst=[["a", "b", "c"], ["d", "e", "f"], ["g","h"]]
check="a"
print ["{} {}".format(index1,index2) for index1,value1 in enumerate(lst) for index2,value2 in enumerate(value1) if value2==check]
输出:
['0 0']
步骤:
- 我已经枚举了 List of List 并得到了它的索引和列表
- 然后我枚举了得到的列表并检查它是否与
check
变量匹配,如果匹配则将其写入列表
这给出了所有可能的输出
即)
代码2:
lst=[["a", "b", "c","a"], ["d", "e", "f"], ["g","h"]]
check="a"
print ["{} {}".format(index1,index2) for index1,value1 in enumerate(lst) for index2,value2 in enumerate(value1) if value2==check]
给出:
['0 0', '0 3']
备注:
- 如果需要,您可以轻松地将其转换为列表列表而不是字符串
这就够了吗?
array = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
for subarray in array:
if 'a' in subarray:
print(array.index(subarray), '-', subarray.index('a'))
这将 return 0 - 0。第一个零是数组内子数组的索引,最后一个零是子数组内的 'a' 索引。
修改了 Bhrigu Srivastava 的提案:
def findinlst(lst, val):
for x in range(0, len(lst)):
try:
pos = lst[x].index(val)
return [x, pos]
except:
continue
return [False, False] # whatever one wants to get if value not found
arr = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
findinlst(arr, 'b')
[0, 1]
findInLst(arr, 'g')
[2, 0]
findinlst(arr, 'w')
[False, False]
我正在尝试获取 python 中嵌套列表中元素的索引 - 例如 [[a, b, c], [d, e, f], [g,h]]
(并非所有列表的大小都相同)。
我试过使用
strand_value= [x[0] for x in np.where(min_value_of_non_empty_strands=="a")]
但这只返回一个空列表,即使该元素存在。知道我做错了什么吗?
def find_in_list_of_list(mylist, char):
for sub_list in mylist:
if char in sub_list:
return (mylist.index(sub_list), sub_list.index(char))
raise ValueError("'{char}' is not in list".format(char = char))
example_list = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
find_in_list_of_list(example_list, 'b')
(0, 1)
假设你的列表是这样的:
lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g','h']]
list_no = 0
pos = 0
for x in range(0,len(lst)):
try:
pos = lst[x].index('e')
break
except:
pass
list_no = x
list_no
给出列表编号,pos
给出列表中的位置
您可以使用列表理解和枚举来做到这一点
代码:
lst=[["a", "b", "c"], ["d", "e", "f"], ["g","h"]]
check="a"
print ["{} {}".format(index1,index2) for index1,value1 in enumerate(lst) for index2,value2 in enumerate(value1) if value2==check]
输出:
['0 0']
步骤:
- 我已经枚举了 List of List 并得到了它的索引和列表
- 然后我枚举了得到的列表并检查它是否与
check
变量匹配,如果匹配则将其写入列表
这给出了所有可能的输出
即)
代码2:
lst=[["a", "b", "c","a"], ["d", "e", "f"], ["g","h"]]
check="a"
print ["{} {}".format(index1,index2) for index1,value1 in enumerate(lst) for index2,value2 in enumerate(value1) if value2==check]
给出:
['0 0', '0 3']
备注:
- 如果需要,您可以轻松地将其转换为列表列表而不是字符串
这就够了吗?
array = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
for subarray in array:
if 'a' in subarray:
print(array.index(subarray), '-', subarray.index('a'))
这将 return 0 - 0。第一个零是数组内子数组的索引,最后一个零是子数组内的 'a' 索引。
修改了 Bhrigu Srivastava 的提案:
def findinlst(lst, val):
for x in range(0, len(lst)):
try:
pos = lst[x].index(val)
return [x, pos]
except:
continue
return [False, False] # whatever one wants to get if value not found
arr = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
findinlst(arr, 'b')
[0, 1]
findInLst(arr, 'g')
[2, 0]
findinlst(arr, 'w')
[False, False]