How to get list of keys that share a value with another key within the same dictionary?

2024/11/20 7:10:32

I have a dictionary of unique keys where some keys share the same value.

For example:

D = {'ida':{'key':'1'},'idb':{'key':'2'},'idc':{'key':'3'},'idd':{'key':'3'},'ide':{'key':'4'},'idf':{'key':'4'},'idg':{'key':'4'}}

I want a list of keys that share the same value with other keys.

In this case, it would be

l = ['idc','idd','ide','idf','idg']

However, I want to exclude one key from all sets of keys that share the same value.

For example, I'd like to have the keys

l = ['idd','idf','idg']

which excludes 'idc' and 'ide'

or it could be

l = ['idc','ide','idf']

which excludes 'idd' and 'idg'.

Answer

If the value appears more than once the list comp will add the key.

dup_keys = [k for k in D if sum(D[k] in x for x in D.iteritems()) > 1 ]['idf', 'idg', 'idd', 'ide', 'idc']dup_keys[1:]
['idg', 'idd', 'ide', 'idc']
dup_keys[1:-1]
['idg', 'idd', 'ide']

if sum(D[k] in x for x in D.iteritems()) > 1 checks the value appears > 1 times.

To ignore certain keys add some and condition, I am not sure on what basis you want to ignore the keys.

https://en.xdnf.cn/q/119859.html

Related Q&A

Sqlite3 Error: near question mark: syntax error [duplicate]

This question already has answers here:Parameterized query binding table name as parameter gives error(3 answers)How to use variables in SQL statement in Python?(5 answers)Closed last year.I am trying…

running bs4 scraper needs to be redefined to enrich the dataset - some issues

got a bs4 scraper that works with selenium - see far below: well - it works fine so far: see far below my approach to fetch some data form the given page: clutch.co/il/it-services To enrich the scrap…

Uploading a file in a embed discord.py (not a image)

Im trying to upload a file directly in a embed, I can upload the file but I dont find the way to put it in the embed. What I want is not displaying the file but uploading it so we can download it, is i…

Cannot install psycopg2 on virtualenv

Hi I use manjaro Linux and I tryed to install psycopg2 packge inside virtualenv but it gave errror error: command gcc failed with exit status 1. Then in the console I tryed gcc --version it saidbash: …

how to execute different print function based on the users input

I am a complete beginner to coding and python so It is probably very simple. So my problem is that am learning how to put if and else function based on the users input and i dont know how to connect be…

matplotlib - AttributeError: module numbers has no attribute Integral

I am a newbie to python and i am trying to learn online. I tried importing matplotlib on python 3.6 but i keep getting this error:problem in matplotlib - AttributeError: module numbers has no attribute…

How to extract social information from a given website?

I have a Website URL Like www.example.comI want to collect social information from this website like : facebook url (facebook.com/example ), twitter url ( twitter.com/example ) etc., if available anywh…

Check if string is of nine digits then exit function in python

I have a function in python that returns different output of strings (Text). And I have different parts that I should check for the string and if the string is of nine digits or contains 9 digits then …

How to extract quotations from text using NLTK [duplicate]

This question already has answers here:RegEx: Grabbing values between quotation marks(20 answers)Closed 8 years ago.I have a project wherein I need to extract quotations from a huge set of articles . H…

takes exactly 2 arguments (1 given) when including self [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…