473,386 Members | 1,799 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,386 software developers and data experts.

Create dictionary from another dictionary

GTXY20
29
Hello;

I have the following dictionary:

Expand|Select|Wrap|Line Numbers
  1. d={1:[a,b,b], 2:[a,a,b,c], 3:[a,b,c], 4:[a,c]}
I am having trouble removing the duplicate values in each of the keys value list, I need it so that:

[code]d={1:[a,b], 2:[a,b,c], 3:[a,b,c], 4:[a,c]}{/CODE]

I was thinking of that I would need to import the sets module???
Oct 4 '07 #1
4 2062
bvdet
2,851 Expert Mod 2GB
Hello;

I have the following dictionary:

Expand|Select|Wrap|Line Numbers
  1. d={1:[a,b,b], 2:[a,a,b,c], 3:[a,b,c], 4:[a,c]}
I am having trouble removing the duplicate values in each of the keys value list, I need it so that:

[code]d={1:[a,b], 2:[a,b,c], 3:[a,b,c], 4:[a,c]}{/CODE]

I was thinking of that I would need to import the sets module???
Are you in Python 2.3? Here is one way of doing it:
Expand|Select|Wrap|Line Numbers
  1. dd = {1:['a','b','b'], 2:['a','a','b','c'], 3:['a','b','c'], 4:['a','c']}
  2.  
  3. for item in dd.items():
  4.     # Python 2.3
  5.     dd[item[0]] = [i for i in item[1] if i not in locals()['_[1]'].__self__]
  6.     # Python 2.4
  7.     # dd[item[0]] = [i for i in item[1] if i not in locals()['_[1]']]
  8.  
  9. print dd
  10.  
  11. # >>> {1: ['a', 'b'], 2: ['a', 'b', 'c'], 3: ['a', 'b', 'c'], 4: ['a', 'c']}
If you are in 2.3, import Set from sets like this:
Expand|Select|Wrap|Line Numbers
  1. >>> from sets import Set as set
...and it will seem like you are in 2.4.
Oct 4 '07 #2
GTXY20
29
Thanks!

I am actually using PythonWin 2.5.1.

Will this cause any problems - I am away from my workstation.
Oct 4 '07 #3
bvdet
2,851 Expert Mod 2GB
Thanks!

I am actually using PythonWin 2.5.1.

Will this cause any problems - I am away from my workstation.
Not that I am aware of. Try the 2.4 version. If that does not work or if you would prefer using set(), use list(set([a,a,b,c])) instead. set() is built-in - no need to import.
Oct 4 '07 #4
GTXY20
29
2.4 version works great!

Thank you.
Oct 4 '07 #5

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

Similar topics

4
by: Derek Fountain | last post by:
I'm just starting another PHP project and can see a familiar task not far on the horizon. I have several database record tuples that I need to manipulate - person, department, client, job, etc. -...
12
by: Antoon Pardon | last post by:
Well at least I find them missing. For the moment I frequently come across the following cases. 1) Two files, each with key-value pairs for the same dictionary. However it is an error if the...
9
by: superprad | last post by:
"X-No-Archive: yes" what I am looking for is 1. To create a list of different words of various lengths(1-15) using A-Z,a-z,0-9 and punctuations.Basically anything that could be found on a...
8
by: Rodd Snook | last post by:
I have an application which makes extensive use of the Scripting.Dictionary object. I'm not doing anything silly like putting them outside the page scope -- just creating quite a few of them and...
3
by: shrishjain | last post by:
Hi All, I have to make multiple dictionary objects(Dictionary<string key, int value>) with same set of string keys, and that really eats up lot of space. I am trying to find a solution where I...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
10
by: Ben | last post by:
Hello... I have set up a dictionary into whose values I am putting a list. I loop around and around filling my list each time with new values, then dumping this list into the dictionary. Or so I...
91
by: jerger | last post by:
I want to help teach to a minority group in Milwaukee, so I want to create a dictionary program that translates a sentence (like a homework problem or teacher instructions), from English into Hmong....
2
by: lazy | last post by:
Hi, I have a dictionary something like this, key1=>{key11=> , key12=> , .... } For lack of wording, I will call outer dictionary as dict1 and its value(inner dictionary) dict2 which is a...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.