Python Hashtable doesn’t save the list slice I passed to it

Python Hashtable doesn’t save the list slice I passed to it … here is a solution to the problem.

Python Hashtable doesn’t save the list slice I passed to it

This is a basic Python script that parses strings and retrieves parameters and their values.

import re

link = "met_y=population&fdim_y=patientStatus:7&fdim_y=pregnant:1&scale_y=lin&ind_y=false&rdim=department&idim=department:9:2:4&idim=clinic :93301:91100:93401:41201:41100&ifdim=department&tstart=1190617200000&tend=1220511600000&ind=false&draft"

print link

filters = ''

matches = re.findall("\&?(? P<name>\w+)=(? P<value>(\w|:) +)\&?",link )
for match in matches:
    name = match[0]
    value = match[1]
    selection = value.split(':')

filters = {}
    print selection[0]
    print selection[1:len(selection)]
    filters[selection[0]] = selection[1:len(selection)]

print filters

The problem here is that the hash table filter never gets these values. The output of this script is

:

{'false': []}

What am I doing wrong?

Solution

You are recreating the filter inside the loop:

filters = {}

This line needs to be placed before the loop, not inside the loop.

Another potential problem is that your input contains duplicate keys (fdim_y and idim). As it stands, your code only keeps the last value of each key.

Related Problems and Solutions