unique values of a Dictionary list (removing duplicate elements of a list)

Chad Kellerman sunckell at gmail.com
Fri May 21 07:20:25 EDT 2010


Python users,
      I am parsing an AIX trace file and creating a dictionary containing
keys (PIDS) and values (a list of TIDS).  With PIDS being unique process ids
and TIDS, being a list of thread ids.  My function populates the keys so
that they are unique, but my list contains duplicates.

     Can someone point me in the right direction so that my dictionary value
does not contain duplicate elements?


here is what I got.

--------------<portion of code that is relevant>------------------

pidtids  = {}

# --- function to add pid and tid to a dictionary
def addpidtids(pid,tid):
    pidtids.setdefault(pid,[]).append(tid)

# --- function to parse a file
def grep(pattern, fileObj, include_line_nums=False):
    r=[]
    compgrep = re.compile(pattern)

    for line_num, line in enumerate(fileObj):
        if compgrep.search(line):
            info = line.split()
            p = info[7].lstrip("pid=")
            t = info[8].lstrip("tid=")
            addpidtids(p,t)


# process trace.int
tf = open(tracefile, 'r')
grep("cmd=java pid",tf)
tf.close()

--------------</portion of code that is relevant>------------------

Any help would be greatly appreciated.


Thanks,
Chad

-- 
A grasshopper walks into a bar and the bartender says "Hey, we have a drink
named after you." And the grasshopper says "Really, You have a drink named
Murray?"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100521/418d68db/attachment.html>


More information about the Python-list mailing list