0

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?

Community
  • 1
  • 1
Ason
  • 509
  • 2
  • 9
  • 25
  • 1
    what is your expected output? dictionary doesn't support `append()` that's why you're getting `AttributeError` . – Ashwini Chaudhary Jul 09 '12 at 19:35
  • I want to have a dictionary with the group number as the key and the objects that are within it as the values. – Ason Jul 09 '12 at 19:36
  • @AshwiniChaudhary: The error talks about an `Element` object. I'm guessing there's a class in the OP's code that's not shown here – inspectorG4dget Jul 09 '12 at 19:37
  • There is a class, but it should have nothing to do with this part of the code! – Ason Jul 09 '12 at 19:39
  • Seeing as you're not well versed in Python, it may very well be that the "Element" object that's referenced in the error is relevant. – Zoran Pavlovic Jul 09 '12 at 19:43
  • @zoranPavlovic I wouldn't doubt it, I added the Element object – Ason Jul 09 '12 at 19:45
  • I'm not saying that is it. It probably isn't related to the problem. But until you can learn to distinguish the irrelevance of secondary information related to your question, perhaps it's better to include more information rather than less. More is never going to hurt. – Zoran Pavlovic Jul 09 '12 at 19:50
  • I will keep that in mind! thank you! now that I look at it, it may have an important side to it... – Ason Jul 09 '12 at 19:54

4 Answers4

6

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).

kindall
  • 178,883
  • 35
  • 278
  • 309
2

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.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
2
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.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
1

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()
Andre Blum
  • 391
  • 3
  • 6
  • 1
    I see from other posts that defaultdict and setdefault are built in solutions for this. Which is why you learn a lot even by answering :-) – Andre Blum Jul 09 '12 at 19:43