Ason
Ason

Reputation: 509

Multiple values to one key in python

So my main goal is simple, I want multiple values to be returned when using a single key. However I'm getting errors and confusing behavior. I am new to Python, so I fully expect there to be a simple reason for this issue.

I have a list of objects, list which only contains the index of the object. i.e.

1
2
3
4 
etc.. 

and a file containing the groups that each of the objects belong to, listed in the same order. The file is a single value for n lines (n being the length of the list of objects as well.) i.e. the file looks like this:

2
5
2
4
etc..

meaning the first object belongs in group 2, the second object in group 5, the third in group 2 and fourth in group 4. This file will change depending on my input files. I have attempted the two following suggestions (that I could find).

EDIT: my end goal: to have a dictionary with group numbers as the keys and the objects in the groups as the values.

I looked to this StackOverflow question first for help since it is so similar, and ended up with this code:

def createdDict(list, file): 

    f = open(file, 'r')
    d={}
    i=0 

    for line in f:
        groupIndex = int(line)
        if groupIndex in d:
            d[groupIndex].append(list[i])
        else:
            d[groupIndex] = list[i]
        i +=1
    print d
    f.close()

And this error:

AttributeError: 'Element' object has no attribute 'append'

d[groupIndex] is just a dictionary and its key and groupIndex should also just be an integer.. not an object from a class I created earlier in the script. (Why is this error showing up?)

I then revised my code after coming upon this other question to be like the following, since I thought this was an alternative way to accomplish my task. My code then looked like this:

def createdDict(list, file): 

    f = open(file, 'r')
    d={}
    i=0 

    for line in f:
        groupIndex = int(line)
        if groupIndex in d:
            d.setdefault('groupIndex', []).append(list[i])
        else:
            d[groupIndex] = list[i]
        i +=1
    print d
    f.close()

This code snippet doesn't end in an error or what I want, but rather (what I believe) are the last objects in the groups... so print d gives me the key and the last object placed in the group (instead of the desired: ALL of the objects in that group) and then terminal randomly spits out groupIndex followed by all of the objects in list.

My question: what exactly am I missing here? People upvoted the answers to the questions I linked, so they are most likely correct and I am likely implementing them incorrectly. I don't need a correction to both procedures, but the most efficient answer to my problem of getting multiple values attached to one key. What would be the most pythonic way to accomplish this task?

EDIT 2: if this helps at all, here is the class that the first method is referencing the error too. I have no idea how it defined any part of this code as a part of this class. I haven't really developed it yet, but I'm all for an answer, so if this helps in locating the error:

class Element(object):
    def __init__(self, globalIndex):
        self.globalIndex = globalIndex
    def GetGlobalIndex (self):
        return self.globalIndex

globalIndex is a separate index of objects (Elements). with my current problem, I am taking a list of these Elements (this is the list mentioned earlier) and grouping them into smaller groups based upon my file (also mentioned earlier). Why I thought it shouldn't matter, the list is essentially a counting up of integers... How would it mess with my code?

Upvotes: 0

Views: 12543

Answers (4)

kindall
kindall

Reputation: 184141

The base of your problem is in this line:

d[groupIndex] = list[i]

In other words, when a key is not in the dictionary, you add a single value (Element object) under that key. The next time you see that key, you try to append to that single value. But you can't append to single values. You need a container type, such as a list, to append. Python doesn't magically turn your Element object into a list!

The solution is simple. If you want your dictionary's values to be lists, then do that. When you add the first item, append a one-element list:

d[groupIndex] = [list[i]]

Alternatively, you can take a one-item slice of the original list. This will be a list already.

d[groupIndex] = list[i:i+1]

Now the dictionary's values are always lists, and you can append the second and subsequent values to them without error.

As ecatmur points out, you can further simplify this (eliminating the if statement) using d.setdefault(groupIndex, []).append(list[i]). If the key doesn't exist, then the value is taken to be an empty list, and you simply always append the new item. You could also use collections.defaultdict(list).

Upvotes: 6

Andre Blum
Andre Blum

Reputation: 391

In your first try, you seem to correctly understand that adding the first element to the dictionary item is a special case, and you cannot append yet, since the dictionary item has no value yet.

In your case you set it to list[i]. However, list[i] is not a list, so you cannot run append on it in later iterations.

I would do something like:

for line in f:
    groupIndex = int(line)
    try:
        blah = d[groupIndex] # to check if it exists
    except:
        d[groupIndex] = [] # if not, assign empty list

    d[groupIndex].append(list[i])
print d
f.close()

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142136

from itertools import izip
from collections import defaultdict

dd = defaultdict(list)
with open('filename.txt') as fin:
    for idx, line in izip(my_list, fin):
        num = int(line)
        defaultdict[num].append(idx)

This creates a defaultdict with a default type of list, so you can append without using setdefault. Then reads each element of my_list combined with the corresponding line from the file, converts the line to an integer, then adds to the group (represented by num) the corresponding index.

Upvotes: 2

ecatmur
ecatmur

Reputation: 157334

Just use

d.setdefault(groupIndex, []).append(list[i])

This will check whether groupIndex is in d, so you don't need the if groupIndex in d: line.

Upvotes: 2

Related Questions