Search code examples
pythondefaultdict

how to store lists as values in python defaultdict lists


Is there a way to store coordinates as tuples or lists within a dictionary ? I have created a dictionary that holds the (distance from origin) as key and (coordinates in x,y) as values. But, if I try to append multiple coordinates to the same distance, it overwrites.

Here is my code so far:

import math
from collections import defaultdict
origin_x = 0.0
origin_y = 0.0

P = [(0,1),(1,0),(1,0)]
k = 2

xmid = float(sum(P[:][0]))/len(P)

xleft = [x for x in P[:][0] if x <= xmid]
xright = [x for x in P[:][0] if x > xmid]

Pleft = [(x,y) for x,y in P[:] if x <= xmid ]
Pright = [(x,y) for x,y in P[:] if x > xmid]

print Pleft

def dist(x1,y1):
    return math.sqrt((x1-origin_x)**2 + (y1-origin_y)**2)

worst = []
nearest = defaultdict(list)

for l in xrange(0,len(Pleft)):
    curr = dist(Pleft[l][0], Pleft[l][1])
    print curr
    if len(nearest) > 1:
        worst[0] = sorted(nearest.keys())[len(nearest)]

    if len(nearest) <= k:
        nearest[curr]= Pleft[l]
    else:
        if curr < worst[0]:
            nearest[curr].append(Pleft[l])
        else:
            break
    #nearest
        #best[0] = curr
        #best[1] = (Pleft[l])

for r in xrange(0,len(Pright)):
    curr = dist(Pright[r][0],Pright[r][1])
    if len(nearest) > 1:
        worst[0] = sorted(nearest.keys())[len(nearest)-1]
    if len(nearest) <= k:
        nearest[curr] = Pright[r]
    else:
        if curr < worst[0]:
            nearest[curr].append(Pright[r])
        else:
            break

print nearest

The below code works:

>> nearest[2.99].append([2,3])

>> nearest
Out[70]: defaultdict(<type 'list'>, {1.0: [1, 0], 2.0: [1, 2], 4.8: '[3, 1]', 2.99: [[2, 3]]})

Solution

  • can you try this code? I've basically made the change of nearest[curr].append(P[left) instead of nearest[curr] = p[left]...which replaces the list with a tuple... Also you don't really have to do x for x in P[:][0], you could just do:

    x for x in P[0]
    

    Try this and let me know if it solves your problem....

    from collections import defaultdict
    origin_x = 0.0
    origin_y = 0.0
    
    P = [(0,1),(1,0),(1,0)]
    k = 2
    
    xmid = float(sum(P[0]))/len(P)
    
    xleft = [x for x in P[0] if x <= xmid]
    xright = [x for x in P[0] if x > xmid]
    
    Pleft = [(x,y) for x,y in P if x <= xmid ]
    Pright = [(x,y) for x,y in P if x > xmid]
    
    print Pleft
    
    def dist(x1,y1):
        import math
        return math.sqrt((x1-origin_x)**2 + (y1-origin_y)**2)
    
    worst = []
    nearest = defaultdict(list)
    
    for l in xrange(0,len(Pleft)):
        curr = dist(Pleft[l][0], Pleft[l][1])
        print curr
        if len(nearest) > 1:
            worst[0] = sorted(nearest.keys())[len(nearest)]
    
        if len(nearest) <= k:
            nearest[curr].append(Pleft[l])
        else:
            if curr < worst[0]:
                nearest[curr].append(Pleft[l])
            else:
                break
        #nearest
            #best[0] = curr
            #best[1] = (Pleft[l])
    
    for r in xrange(0,len(Pright)):
        curr = dist(Pright[r][0],Pright[r][1])
        if len(nearest) > 1:
            worst[0] = sorted(nearest.keys())[len(nearest)-1]
        if len(nearest) <= k:
            nearest[curr].append(Pright[r])
        else:
            if curr < worst[0]:
                nearest[curr].append(Pright[r])
            else:
                break
    
    print nearest