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)
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: - dd = {'file1.txt': ['A', 'B' , 'C' , 'D' , 'E' ] ,
-
'file2.txt': ['A', 'F' , 'C' , 'G' , 'E' ] ,
-
'file3.txt': ['T', 'F' , 'C']}
-
-
# Create a dictionary with a count of labels
-
dd1 = {}
-
for seq in dd.values():
-
for label in seq:
-
v = dd1.get(label, 0)
-
dd1[label] = v+1
-
-
for key in dd:
-
dd[key] = [label for label in dd[key] if dd1[label] == 1]
-
print dd
-
-
-
dd = {'file1.txt': ['A', 'B' , 'C' , 'D' , 'E' ] ,
-
'file2.txt': ['A', 'F' , 'C' , 'G' , 'E' ] ,
-
'file3.txt': ['T', 'F' , 'C']}
-
-
# Create an extended list from dd.values()
-
extended = dd.values()[0][:]
-
for seq in dd.values()[1:]:
-
extended.extend(seq[:])
-
for key in dd:
-
dd[key] = [label for label in dd[key] if extended.count(label) == 1]
-
print dd
Output: - >>> {'file1.txt': ['B', 'D'], 'file3.txt': ['T'], 'file2.txt': ['G']}
-
{'file1.txt': ['B', 'D'], 'file3.txt': ['T'], 'file2.txt': ['G']}
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |