【发布时间】:2022-08-13 21:23:29
【问题描述】:
我是新手,我想从 Python 的列表中的列表中找到特定的项目索引。 它仅适用于一个列表中的多个列表,我想知道您是否知道很简单的搜索方法,以查找所有现有和新添加的列表以查找项目的特定索引,而不是在两个列表中。 我会很乐意提供帮助。
main_list = [[0, 1, 1, 3], [1, \'b\', \'c\', \'d\'], [0, \'b\', 1, \'d\']]
print(f\'MAIN LIST: {main_list}\')
find_in_list = False # put number of list if specific search is needed ### find_in_list = 1
find_item = \'b\' # item that you want to find in one or multiple lists
count_number_of_lists = -1
add_item_index = []
store_items_index = []
item_index_of_store_items_index = 0
list_index_and_item_index = []
for sub_list_index, sub_list_others in enumerate(main_list):
count_number_of_lists += 1
if find_in_list == False:
sub_list = main_list[sub_list_index] # no specific list
for sub_list_index_2, sub_list_others_2 in enumerate(sub_list):
if sub_list_others_2 == find_item: # find exactly same item as needed to be found
add_item_index.append(sub_list_index_2)
print(add_item_index)
else:
sub_list = main_list[find_in_list] # specific list
for sub_list_index_2, sub_list_others_2 in enumerate(sub_list):
if sub_list_others_2 == find_item: # find exactly same item as needed to be found
add_item_index.append(sub_list_index_2)
store_items_index.append(add_item_index)
add_item_index = []
for sub_index, sub_other in enumerate(store_items_index):
item_index_of_store_items_index = store_items_index[sub_index]
list_index_and_item_index.append([count_number_of_lists, item_index_of_store_items_index])
if find_in_list == False:
print(f\'LIST INDEX: {count_number_of_lists}, ITEM INDEX: {item_index_of_store_items_index}\')
if find_in_list == False:
print(f\'List of [LIST INDEX and, [ITEM INDEX]]: {list_index_and_item_index}\') # all LIST INDEX and ITEM INDEX in one list
print(f\'ALL ITEMS INDEX: {store_items_index}\') # all index of items in one list
else:
print(f\'LIST INDEX: {count_number_of_lists}, ITEM INDEX: {item_index_of_store_items_index}\')