簡體   English   中英

有效地為嵌套的元組列表找到匹配的索引和值

[英]Efficiently find matching indices and values for nested list of tuples

我正在處理不同大小的列表,我的問題是:

假設我有兩個不同大小的元組列表:

value1 = [(0, 1), (0, 2), (0, 3)]
value2 = [(0, 6), (0, 2), (0, 4), (0, 9), (0, 7)]

插入另一個列表:

my_list = [value1, value2]

mylist附加到third list並按順序返回時,找到匹配索引的最有效方法是什么(最好是 O(n) )? 結果應該類似於:

value3 = [(0, 1), (0, 2), (0, 3), (0, 5), (0, 7), (0, 10)]

mathing_values (my_list, value3): 
    
    my_list.append(value3)

    return ->  "The List 'value3' has a matching with 'value1' in 
                index 0 : (0, 1), index 1: (0, 2) and with 'value2'
                in index 4: (0, 7)"

Obs:如果它適用於多個列表(超過 3 個),那將是完美的

我不確定這是最有效的方法,但可讀且簡單:

v3 = set(value3)

[set(x).intersection(v3) for x in my_list]

UPD:使用 dict 值作為索引的擴展解決方案:

v3 = set(value3)

[(i, k) for x in my_list for (i, k) in enumerate(set(x)) if k in v3]

我不認為set()會起作用,因為你想要索引,所以這不是超級有效,但它會起作用:

def find_matches(my_list, new_list):
    indices = []
    values = []
    for index, (a, b) in enumerate(zip(my_list[0], my_list[1])):
        for new_value in new_list:
            if new_value == a or new_value == b:
                indices.append(index)
                values.append(new_value)
    if len(indices) == 0:
        return "No matches"
    else:
        return "Matching Values: {} Corresponding Indices: {}".format(values, indices)

然后只需調用 function:

print(find_matches(my_list, value3))

output:

匹配值:[(0, 1), (0, 2), (0, 3)] 對應索引:[0, 1, 2]

這是 pandas 的解決方案,它會更快,並且可以包含任意數量的列表。 希望這可以幫助。

import pandas as pd

def find_matches(my_list, new_list):
    
    #create a dataframe from the lists contained in my_list
    dfs = []
    for l in my_list:
        l_series = pd.Series(l)
        l_df = pd.DataFrame(l_series)
        dfs.append(l_df)
    df = pd.concat(dfs, ignore_index=True, axis=1)
    
    #create second df of Boolean values if there are matches to the new_list
    df2 = df[df.isin(new_list)]
    df_final = df2.dropna(how='all')#drop rows where no matches were found in any list

    
    return df_final

調用:

find_matches(my_list, value3)

返回:

    0   1
0   (0, 1)  NaN
1   (0, 2)  (0, 2)
2   (0, 3)  NaN
4   NaN (0, 7)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM