簡體   English   中英

比較字典中某個鍵的元素內部值等於下一個鍵

[英]Comparing element inside value of a key in a dictionary is equal to the next key

d = {
    0:{1,2,3},
    1:{567},
    2:{2,3,5,8},
    3:{4,5,7,9},
    4:{6,7,8}
    }

我想將第一個kv對的值與下一個kv對的鍵進行比較。

示例:要檢查{1,2,3}是否存在1{567}是否存在2 ,如果確實存在,那么我想刪除值中存在的k。

輸出應如下所示:

d = {
    0:{1,2,3},
    2:{2,3,5,8}
    }

我嘗試使用具有各種排列和組合的字典迭代器,但沒有結果。 達到結果的最佳方法是什么?

Python字典不是按順序排列的,因此我不確定您在這里真的可以說“上一個”的“下一個”。 使用pandas系列會更合適。

但是,您仍然可以遍歷鍵並將其定義為訂單。

previous = {}
dict_items = list(d.items())
for k,v in dict_items:
    if k in previous:
        del d[k]
    previous = v

編輯:確保鍵的順序正確,更改dict_items為:

dict_items = sorted(d.items(),key=lambda x:x[0])

會做到的

根據您的示例和要求進行猜測,您使用的是Python 3.6+,這些字典會保留插入順序。 你可以做:

In [57]: d = { 
    ...:     0:{1,2,3}, 
    ...:     1:{567}, 
    ...:     2:{2,3,5,8}, 
    ...:     3:{4,5,7,9}, 
    ...:     4:{6,7,8} 
    ...:     }                                                                                                                                                                                              

# Get an iterator from dict.keys
In [58]: keys_iter = iter(d.keys())                                                                                                                                                                         

# Get the first key
In [59]: first_key = next(keys_iter)                                                                                                                                                                        

# Populate output dict with first key-value
In [60]: out = {first_key: d[first_key]}                                                                                                                                                                    

# Populate out dict with key-values based on condition by
# looping over the `zip`-ed key iterator and dict values 
In [61]: out.update({k: d[k] for k, v in zip(keys_iter, d.values())
                     if k not in v})                                                                                                                         

In [62]: out                                                                                                                                                                                                
Out[62]: {0: {1, 2, 3}, 2: {2, 3, 5, 8}}

我花了一段時間才弄明白你想要什么。 在下面,我重新措辭了您的示例:

    EXAMPLE:

    Suppose the input dictionary is as follows:
       0:{1,2,3},    
       1:{567},      
       2:{2,3,5,8},  
       3:{4,5,7,9},  
       4:{6,7,8}    

    We have...

          key of 0:{1,2,3}  is 0
        value of 0:{1,2,3} is {1,2,3}

          key of 2:{2,3,5,8} is 2
        value of 2:{2,3,5,8} is {2,3,5,8}

    We execute code similar to the following:

    if key of 1:{567} in value of 0:{1,2,3}: 
    # if 1 in {1,2,3}:    
        delete 1:{567}

    if key of 2:{2,3,5,8} in value of 1:{567}:
    # if 2 in {567}: 
        delete 2:{2,3,5,8}

    and so on...    
    """

以下代碼應該可以實現您的目標:

def cleanup_dict(in_dict):  

    # sort the dictionary keys
    keys = sorted(indict.keys())

    # keys_to_delete will tell us whether to delete
    # an entry or not
    keys_to_delete = list()

    try:
        while True:
            prv_key = next(keys)
            nxt_key = next(keys)
            prv_val = in_dict[prv_key]
            if (nxt_key in prv_val):
                keys_to_delete.append(nxt_key)
    except StopIteration:
        pass 

    for key in keys_to_delete:
        del in_dict[key]            
    return

暫無
暫無

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

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