Results 1 to 4 of 4

Thread: Help with creating dictionary of this structure in Python

  1. #1
    Join Date
    Nov 2009
    Beans
    8

    Help with creating dictionary of this structure in Python

    I'm having trouble creating a dictionary of this structure. Based on a .txt file with two columns, I need to create a dictionary where each key is the first column's entries (but repeats won't appear as keys in the dictionary), and values are second column's entries (matched to the first column's entries). Example, this is what the .txt file looks like:

    A 1
    B 2
    B 1
    A 4
    C 2
    C 3
    A 5
    B 3

    And the dictionary would look like this: there would only be three keys, since only A, B, and C appears on the list (repeats won't appear), and each key matched with a list of values consisting of the entries each key is matched with in the .txt file:
    {'A': [1, 4, 5], 'B': [1, 2, 3], 'C': [2, 3]}

    Thanks in advance if anyone could help me with this!

  2. #2
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with creating dictionary of this structure in Python

    Default dict, I summon thee:

    PHP Code:
    #!/usr/bin/env python

    from collections import defaultdict

    dictfile 
    open("test.txt")
    pairs defaultdict(list)

    for 
    line in dictfile:
        
    keyvalue line.split()
        
    pairs[key].append(value)


    print 
    pairs

    dictfile
    .close() 
    Or if you prefer a normal dictionary, then change then use setdefault method:

    PHP Code:
    #!/usr/bin/env python

    dictfile open("test.txt")
    pairs = {}

    for 
    line in dictfile:
        
    keyvalue line.split()
        
    pairs.setdefault(key, []).append(value)

    print 
    pairs

    dictfile
    .close() 
    The first one is faster, the second one is more natural.

    *oh, you wanted to avoid repeated values too? Then swap the [] for a set().
    Last edited by Can+~; November 11th, 2009 at 06:02 AM.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  3. #3
    Join Date
    Nov 2009
    Beans
    8

    Re: Help with creating dictionary of this structure in Python

    Oh wow the second one worked perfectly. Thank you.
    Could you please explain what it did, especially the "setdefault" part?
    Oh and I was just wondering, when I was thinking about this problem I had the idea to just use a for loop to iterate over each pair of entries in the .txt file. Is it possible to create the same dictionary using for/while loops?

  4. #4
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with creating dictionary of this structure in Python

    Quote Originally Posted by Tradewind View Post
    Oh wow the second one worked perfectly. Thank you.
    Could you please explain what it did, especially the "setdefault" part?
    Code:
    mydict.setdefault(key, value)
    setdefault gets the current element under key (passed as the first argument), and if not present, it will first add a default element (second argument), and then return it.

    Code:
    mydict.setdefault(key, value).append(...)
    Therefore: If the key is there, then just return it, and we append to it. If it is not, first create a list inside, then return it, and we append to it. Easy.

    Quote Originally Posted by Tradewind View Post
    Is it possible to create the same dictionary using for/while loops?
    Well, yeah:

    PHP Code:
    #!/usr/bin/env python

    dictfile open("test.txt")
    pairs = {}

    for 
    line in dictfile:
        
    keyvalue line.split()
        
        
    # If key isn't in there, create a new one with an empty list
        
    if key not in pairs:
            
    pairs[key] = []
        
        
    # Now append
        
    pairs[key].append(value)

    print 
    pairs

    dictfile
    .close() 
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •