0

I have a class called Polynomial, and it stores information to the polynomial in a dictionary. The keys are the exponents and the values are the coefficients. To create an instance of the Polynomial class, you type the following:

P1 = Polynomial((2, 14), (2, 11), (-12, 3), (42, 0))

The first item in the pairing is the coefficient, and the second item is the exponent.

The initialization looks like this:

class Polynomial:
def __init__(self, *termpairs):
    termdict = dict(termpairs)
    self.termdict = {}
    for x, y in termdict.items():
        self.termdict[y] = x

Now if I have two coefficients that are the same, for example ((2, 14), (2, 11)), It will only create a dictionary for one of them like so:

{11: 2}

Im not sure why the one pairing is not showing up. Any thoughts?

me45
  • 1,059
  • 3
  • 14
  • 16
  • 1
    You have the exponent/coefficient pairs properly matched to key/value pairs when you posted two days ago. What happened? http://stackoverflow.com/questions/8290323/python-subclasses – Raymond Hettinger Nov 30 '11 at 06:18

6 Answers6

2

A dictionary has one value per key. What would self.termdict[11] return otherwise?

kichik
  • 33,220
  • 7
  • 94
  • 114
2

The problem is here:

termdict = dict(termpairs)

You are "converting" your termpairs to a dict, so you will get something like:

((2, 14), (2, 11), (-12, 3), (42, 0)) => {2: 14, 2: 11, -12: 3, 42: 0}

In a dict there cannot be two items with the same key, so the second one (2: 11) is overwriting the first one (2: 14).

EDIT: Why are you converting it to a dict anyway? This would be a simpler solution (with no dict conversion):

def __init__(self, *termpairs):
    self.termdict = {}
    # termpairs is a tuple of tuples
    for x, y in termdict:
        self.termdict[y] = x
juliomalegria
  • 24,229
  • 14
  • 73
  • 89
2

Because your dictionary termdict (not self.termdict) maps coefficients to powers, then the same coefficient forces only the second one to be used (i.e., (2, 11) erases (2, 14)). Instead, you want to replace your class with:

class Polynomial:
    def __init__(self, *termpairs):
        termdict = dict([ (b, a) for a, b in termpairs ])
        self.termdict = {}
        for x, y in termdict.items():
            self.termdict[x] = y
user
  • 7,123
  • 7
  • 48
  • 90
1

This is your culprit:

termdict = dict(termpairs)

This creates a dictionary where the keys are the coefficients and the values are the exponents. Since it's a dictionary, each key points to one value, so at this point you lose the (2, 14) pair -- there can't be more than one item with the key 2. That you then proceed to switch 'em around makes no difference; you've already dropped some data on the floor.

I'm not sure why you're putting them in a dictionary to begin with, and then iterating over that to make a second dictionary. Why not just put them in the way you want to begin with?

self.termdict = dict(reversed(pair) for pair in termpairs)
kindall
  • 178,883
  • 35
  • 278
  • 309
0

You're overwriting the older values each time you add to the dictionary, you need to do something such as adding the values into a list and storing that list as the value.

the dictionary structure would look like:

{ 2:[14,11], -12:[3], 42:[0] }
and will allow you to store multiple pairs.

Serdalis
  • 10,296
  • 2
  • 38
  • 58
0

You don't need to create the termdict dictionary, just iterate over the items of the tuple (untested):

class Polynomial(object):
    def __init__(self, *termpairs):
        self.termdict = {}
        for x, y in termpairs:
            self.termdict[y] = x
Alok Singhal
  • 93,253
  • 21
  • 125
  • 158