【问题标题】:How to find specific item/s in list/s in Python如何在 Python 中的 list/s 中查找特定的 item/s
【发布时间】: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}\')

    标签: python list


    【解决方案1】:
    main_list = [[0, 1, 1, 3], [1, 'b', 'c', 'd'], [0, 'b', 1, 'd']]
    
    find_in_list = False
    find_item = 'b'
    
    if not find_in_list:
        list_indices = list(range(len(main_list)))
    else:
        list_indices = [find_in_list]
    
    res = []
    for i in list_indices:
        occurrences = []
        for j, e in enumerate(main_list[i]):
            if e == find_item:
                occurrences.append(j)
        res.append([i, occurrences])
    print(res)
    

    印刷

    [[0, []], [1, [1]], [2, [1]]]
    

    【讨论】:

    • 非常感谢,它非常有帮助。我对此仍然很陌生,看看我还需要学习多少才能使我的代码变得更好。谢谢!
    【解决方案2】:

    如果在子列表中找不到“要查找的元素”,我宁愿显示零。也可以通过列表理解来解决此任务:

    lst = [[0, 1, 1, 3], [1, 'b', 'c', 'd'], [0, 'b', 1, 'd']]
    to_find = "b"
    print([[ind, [sub_lst.count(to_find)]] for ind, sub_lst in enumerate(lst)])
    

    输出:

    [[0, [0]], [1, [1]], [2, [1]]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 2014-01-02
      • 2013-06-03
      • 2019-12-28
      • 2011-12-28
      • 2014-01-16
      • 2021-12-12
      相关资源
      最近更新 更多