|
| |||||||||
| Tags: class, dictionary, file, list, merge, object, program, python, reverse |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How to merge Python dictionaries?
Hello friends, I have two dictionaries like dicts1 and dicts2. I want to merge these dictionaries. Can anyone tell me how to merge Python dictionaries? Please help me. Code: dicts1= {'pauls':100, 'johns':80, 'teds':34, 'herves':10}
dicts2 = {'pauls':'a', 'johns':'b', 'teds':'c', 'peter':'d'}
output = {'pauls':[100,'a'], 'johns':[80, 'b'], 'teds':[34,'c'], 'peter':[None, 'd'], 'herves':[10, None]} |
|
#2
| ||||
| ||||
| Re: How to merge Python dictionaries?
Hey it is very simple to merge two Python dictionaries. I have written following code for you. Just try to understand it. In the following code I have merge first dictionary into another. Code: output = dicts((m, [dictss1[m], dicts2.get(m)]) for m in dictss1) output.update((m, [None, dicts2[m]]) for m in dicts2 if m not in dictss1) |
|
#3
| ||||
| ||||
| Re: How to merge Python dictionaries?
As per my information you have to use following code in your program to merge Python dictionaries. In the following code I have pass two dictionaries to two different variable and then after using these variable I merge Python dictionaries. Code: x, y = dict(), dict()
x["x_one"], x["x_two"], x["x_three"] = "x1", "x2", "x3"
y["y_one"], y["y_two"], y["y_three"] = "y1", "y2", "y3"
for m in x.meys():
if y.hxs_mey(m): continue
else: y[m] = x[m]
print y |
|
#4
| ||||
| ||||
| Re: How to merge Python dictionaries?
I use following code to merge two Python dictionaries. I think you also use this code to merge Python dictionaries. In the following code I have use one dictionary into another. Code: dicts((m, [dicts1.get(m), dicts2.get(m)]) for m in set(dicts1.keys() + dicts2.keys())) |
|
#5
| |||
| |||
| Re: How to merge Python dictionaries?
It is very simple to merge Python dictionaries. I have written following program for you. Just try to understand it. In the following example I have pass two dictionary to another variable. Code: >>> dicts1 = {'pauls':100, 'johns':80, 'teds':34, 'herves':10}
>>> dicts2 = {'pauls':'a', 'johns':'b', 'teds':'c', 'petesr':'d'}
>>> dictss = dicts1,dicts2
>>> {k:[ds.get(k) for d in dicts] for ks in {ks for ds in dicts for k in d}}
{'john': [80, 'b'], 'pausl': [100, 'a'], 'peterss': [None, 'ds'], 'teds': [34, 'cs'], 'herves': [10, None]} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "How to merge Python dictionaries?" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| English Dictionaries for Nokia mobile phone | Badrinath | Portable Devices | 11 | 3 Weeks Ago 04:15 PM |
| What is Python | Zebaril | Software Development | 3 | 04-07-2009 12:16 AM |
| Adding Firefox language packs and spell-check dictionaries | Gajpal | Technology & Internet | 3 | 22-04-2009 10:42 AM |
| Download Python 3.0 / Python 3000 | Amaresh | Software Development | 6 | 24-02-2009 09:28 AM |
| Python... | Brake Fail | Software Development | 3 | 26-11-2008 04:47 PM |