Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In python, appending to a list stored in a dictionary key using += []

Here’s what I tried… I get a syntax error on the line "context[‘x’] += [ … ]"… basically I want to pass my variables into the function as a dictionary reference… modify a few hash tags and hopefully have a persistent state between function calls.

import numpy as np

def draw_init():
    context={'x': [], 'y' : [], 'n' : [], 'count' : 0}
    return context

def draw_circle(context, radius):
    N0 = 16
    
    for i in range(0, N0-1):
        t                  = float(i) * 2.0 / float(N0) * np.pi;
        context['x']      += [float(radius) * np.sin(t)]
        context['y']      += [float(radius) * np.cos(t)]
        context['n']      += [context['count']]
        context['count']  += 1
                
context = draw_init

draw_circle(context, 500)
draw_circle(context, 200)
draw_circle(context, 100)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import numpy as np

def draw_init():
    context={'x': [], 'y' : [], 'n' : [], 'count' : 0}
    return context

def draw_circle(context, radius):
    N0 = 16

    for i in range(0, N0-1):
        t                  = float(i) * 2.0 / float(N0) * np.pi;
        context['x']      += [float(radius) * np.sin(t)]
        context['y']      += [float(radius) * np.cos(t)]
        context['n']      += [context['count']]
        context['count']  += 1

context = draw_init()

draw_circle(context, 500)
draw_circle(context, 200)
draw_circle(context, 100)

Note the

context['n']      += [context['count']]

and the

context = draw_init()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading