473,395 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Removing duplicates from a dict

Hi I have a dictionary that contains data like this

dict = {'file1.txt': ['A', 'B' , 'C' , 'D' , 'E' ] , 'file2.txt': ['A', 'F' , 'C' , 'G' , 'E' ] , 'file3.txt': ['T', 'F' , 'C']}

Could some one please help me write a code that could remove the duplicate values and change the file to

dict = {'file1.txt': ['B' , 'D' ] , 'file2.txt': [ 'G' ] , 'file3.txt': [ 'T' ]}

(only the unique values should remain)
Feb 16 '12 #1
1 1522
bvdet
2,851 Expert Mod 2GB
Don't use dict as an identifier. It will mask built-in function dict().

Following are a couple of ways:
Expand|Select|Wrap|Line Numbers
  1. dd = {'file1.txt': ['A', 'B' , 'C' , 'D' , 'E' ] ,
  2.       'file2.txt': ['A', 'F' , 'C' , 'G' , 'E' ] ,
  3.       'file3.txt': ['T', 'F' , 'C']}
  4.  
  5. # Create a dictionary with a count of labels
  6. dd1 = {}
  7. for seq in dd.values():
  8.     for label in seq:
  9.         v = dd1.get(label, 0)
  10.         dd1[label] = v+1
  11.  
  12. for key in dd:
  13.     dd[key] = [label for label in dd[key] if dd1[label] == 1]
  14. print dd
  15.  
  16.  
  17. dd = {'file1.txt': ['A', 'B' , 'C' , 'D' , 'E' ] ,
  18.       'file2.txt': ['A', 'F' , 'C' , 'G' , 'E' ] ,
  19.       'file3.txt': ['T', 'F' , 'C']}
  20.  
  21. # Create an extended list from dd.values()
  22. extended = dd.values()[0][:]
  23. for seq in dd.values()[1:]:
  24.     extended.extend(seq[:])
  25. for key in dd:
  26.     dd[key] = [label for label in dd[key] if extended.count(label) == 1]
  27. print dd
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> {'file1.txt': ['B', 'D'], 'file3.txt': ['T'], 'file2.txt': ['G']}
  2. {'file1.txt': ['B', 'D'], 'file3.txt': ['T'], 'file2.txt': ['G']}
  3. >>> 
Feb 16 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Iain | last post by:
Hi I have inherited a web app with the following table structure, and need to produce a table without any duplicates. Email seems like the best unique identifier - so only one of each e-mail...
20
by: Rubinho | last post by:
I've a list with duplicate members and I need to make each entry unique. I've come up with two ways of doing it and I'd like some input on what would be considered more pythonic (or at least...
6
by: M B HONG 20 | last post by:
Hi all - I was wondering if Javascript has a way to easily remove duplicates from a string. For example, if I had a string: "car truck car truck truck tree post post tree" it should turn...
4
by: Drew | last post by:
I have a permission tracking app that I am working on, and I have made the insert page for it. I am having issues on how to prevent duplicates from getting entered. Currently the interface for...
0
by: makthar | last post by:
In your query use DISTINCT SELECT DISTINCT CITY FROM <tablename> WHERE STATE='<state name>'. This will bring only one of each city from the table. >-----Original Message----- >I'm getting a...
16
by: tyrfboard | last post by:
I've been searching for awhile now on how to remove duplicates from a table within an Access db and have found plenty of articles on finding or deleting duplicates. All I want to do is remove them...
5
by: asgars | last post by:
i have two tables, tab1 having N1 col and tab2 N2 col. now N1 is subset of N2. I need the information from tab2 (having N2) of all rows having the matching entry in N1 in tab1. For this i am...
2
by: sjlung | last post by:
I apologise if this is a trivial question but I have appended three tables in access and within this table, there are duplicate entries. I have tried to set my reference number for this table to be...
7
by: vsgdp | last post by:
I have a container of pointers. It is possible for two pointers to point to the same element. I want to remove duplicates. I am open to which container is best for this. I thought of using...
4
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.