Hi everyone.
I have two dictionaries. In dictionary1 I have numbers as keys and the occurrence of those numbers as values. In dictonary2 I have numbers again as keys, but there are more numbers here than in dictionary1. The values in dictionary2 are the description of the numbers. From dictionary1 I can get an output file where I have the unique numbers in the first column and the occurrence of the numbers in the second column. No I want to get the description of the number from dictionary2 and put it in the third column, like following:

7 11 books (Basically number 7 has occurred 11 times and number 7 represents books)
4 5 fruits
10 15 buses
.
.
How to do this?

Unless I am missing something, look up the key from the first dictionary in the second dictionary.

Here is how I am trying to do it:

# initialize a dictionary: dictionary 1
dd = {}
.
.
.
    dd.setdefault(column3, 0)
    # increment dd[port number] by one
    dd[column3] += 1
.
.
.
ports_name={} # dictionary 2
get_port_name ('Ports', ports_name)

# if the key in dd equals the key (item) in ports_name, insert the value of that item to the third column in the output file:
for key in dd.keys():
    for item in ports_name.keys():
        if key == item:
	    #I want the description to be retrievable by the key in dd and then have the description of the numbers on the third column of the output file and that 
            dd[key][1]= ports_name[item]            

f.close()
outfile.write("\n".join(["%s %s %s" % (key, dd[key][0], dd[key][1]) for key in dd]))
outfile.close()

but it doesn't give any result, not even an error message.

Some comments

initialize a dictionary: dictionary 1
dd = {}

"""  indentation is wrong
"""
    dd.setdefault(column3, 0)
    # increment dd[port number] by one
    dd[column3] += 1

"""  get_port_name() does not return anything so the
     dictionary, ports_name, is empty
     Take a look at "1.1. A first program" 
     cut and paste this link
     http://www.pasteur.fr/formation/infobio/python/ch01.html#d0e115
"""
ports_name={} # dictionary 2
get_port_name ('Ports', ports_name)
 
# if the key in dd equals the key (item) in ports_name, insert the value of that item to the third column in the output file:
for key in dd.keys():
    if key in ports_name:
        print ports_name[key]     ## description

##    for item in ports_name.keys():
##        if key == item:
	    #I want the description to be retrievable by the key in dd and then have the description of the numbers on the third column of the output file and that 
##            dd[key][1]= ports_name[item]            
 
f.close()
outfile.write("\n".join(["%s %s %s" % (key, dd[key][0], dd[key][1]) for key in dd]))
outfile.close()

Thanks a alot woooee. This is the line that did it:

for key in dd.keys():
    for item in ports_name.keys():
        if key == item:
            outfile.write("\n".join(["%s %s %s" % (key, dd[key], ports_name[item])])+ "\n")

Now I want to sort the data on a descending order based on the second column, that is dd[key] which represents the number of appearances of each item. How to do the sorting? Thanks in advance.

Something like this (not able to test without data of your values, but principle should be OK). I use generator instead of lists (sorted produces automatically list).

sorted_table = sorted(((key, dd[key], ports_name[key])
                      for key in dd
                      if key in ports_name), key = lambda x: x[1] )

with open('myoutfile.txt', 'w') as outfile:
    outfile.write('\n'.join(' '.join(line for line in sorted_table)))

Thanks tonyjv. That was great. I just needed to put a minus (-) sign in the lambda function like in the following

if key in ports_name), key = lambda x: -x[1] )

in order to reverse the order.

Alternatively you could have used

reverse = True

parameter.

Now I have an output like this:

[('4672', 8, 'rfa'), ('2800', 8, 'acc-raid'), ('1953', 7, 'rapidbase'), ('2258', 7, 'rcts'), ('2670', 7, 'tve-announce'), ('2273', 5, 'mysql-im'), ('3696', 5, 'telnetcpcd'), ('1818', 5, 'etftp'), ('8080', 5, 'http-alt')]

It is a list of tuples, right? For each tuple in the list, e.g ('4672', 8, 'rfa'),the second entry is the number of occurrences of the first entry and the third entry is a description for the first entry. How do I plot a histogram of the above data with the 'descriptions' along the x-axis and the the 'occurrences' along the y-axis?

How can I write the following list:

[(8, 'rfa'), (8, 'acc-raid'), (7, 'rapidbase'), (7, 'rcts'), (7, 'tve-announce'), (5, 'mysql-im'), (5, 'telnetcpcd'), (5, 'etftp'), (5, 'http-alt')]

to a text file with two columns and many rows, so that I have something like this:

8 rfa
8 acc-raid
7 rapidbase
7 rcts
7 tve-announce
5 mysql-im
5 telnetcpcd

Solved the problem. Thanks anyway guys:)